vilarin commited on
Commit
221b62f
·
verified ·
1 Parent(s): 4c3b0bc

Upload 3 files

Browse files
Files changed (3) hide show
  1. app/webui/app.py +4 -4
  2. app/webui/patch.py +131 -130
  3. app/webui/process.py +1 -1
app/webui/app.py CHANGED
@@ -1,6 +1,6 @@
1
  import re
2
  import gradio as gr
3
- from app.webui.process import model_load, lang_detector, diff_texts, translator, read_doc
4
  from llama_index.core import SimpleDirectoryReader
5
 
6
  def huanik(
@@ -82,9 +82,9 @@ with gr.Blocks(theme="soft", css=CSS) as demo:
82
  endpoint = gr.Dropdown(
83
  label="Endpoint",
84
  choices=["Groq","OpenAI","Cohere","TogetherAI","Ollama","Huggingface"],
85
- value="Groq",
86
  )
87
- model = gr.Textbox(label="Model", value="llama3-70b-8192", )
88
  api_key = gr.Textbox(label="API_KEY", type="password", )
89
  source_lang = gr.Textbox(
90
  label="Source Lang(Auto-Detect)",
@@ -135,7 +135,7 @@ with gr.Blocks(theme="soft", css=CSS) as demo:
135
  output_diff = gr.HighlightedText(visible = False)
136
  with gr.Row():
137
  submit = gr.Button(value="Submit")
138
- upload = gr.UploadButton("Upload")
139
  clear = gr.ClearButton([source_text, output_init, output_reflect, output_final])
140
 
141
  endpoint.change(fn=update_model, inputs=[endpoint], outputs=[model])
 
1
  import re
2
  import gradio as gr
3
+ from .process import model_load, lang_detector, diff_texts, translator
4
  from llama_index.core import SimpleDirectoryReader
5
 
6
  def huanik(
 
82
  endpoint = gr.Dropdown(
83
  label="Endpoint",
84
  choices=["Groq","OpenAI","Cohere","TogetherAI","Ollama","Huggingface"],
85
+ value="OpenAI",
86
  )
87
+ model = gr.Textbox(label="Model", value="gpt-4o", )
88
  api_key = gr.Textbox(label="API_KEY", type="password", )
89
  source_lang = gr.Textbox(
90
  label="Source Lang(Auto-Detect)",
 
135
  output_diff = gr.HighlightedText(visible = False)
136
  with gr.Row():
137
  submit = gr.Button(value="Submit")
138
+ upload = gr.UploadButton("Upload", file_types="text")
139
  clear = gr.ClearButton([source_text, output_init, output_reflect, output_final])
140
 
141
  endpoint.change(fn=update_model, inputs=[endpoint], outputs=[model])
app/webui/patch.py CHANGED
@@ -1,131 +1,132 @@
1
- # a monkey patch to use llama-index completion
2
- from typing import Union, Callable
3
- from functools import wraps
4
- from src.translation_agent.utils import *
5
-
6
-
7
- from llama_index.llms.groq import Groq
8
- from llama_index.llms.cohere import Cohere
9
- from llama_index.llms.openai import OpenAI
10
- from llama_index.llms.together import TogetherLLM
11
- from llama_index.llms.ollama import Ollama
12
- from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
13
-
14
- from llama_index.core import Settings
15
- from llama_index.core.llms import ChatMessage
16
-
17
-
18
- # Add your LLMs here
19
-
20
- def model_load(
21
- endpoint: str,
22
- model: str,
23
- api_key: str = None,
24
- context_window: int = 4096,
25
- num_output: int = 512,
26
- ):
27
- if endpoint == "Groq":
28
- llm = Groq(
29
- model=model,
30
- api_key=api_key,
31
- )
32
- elif endpoint == "Cohere":
33
- llm = Cohere(
34
- model=model,
35
- api_key=api_key,
36
- )
37
- elif endpoint == "OpenAI":
38
- llm = OpenAI(
39
- model=model,
40
- api_key=api_key if api_key else os.getenv("OPENAI_API_KEY"),
41
- )
42
- elif endpoint == "TogetherAI":
43
- llm = TogetherLLM(
44
- model=model,
45
- api_key=api_key,
46
- )
47
- elif endpoint == "ollama":
48
- llm = Ollama(
49
- model=model,
50
- request_timeout=120.0)
51
- elif endpoint == "Huggingface":
52
- llm = HuggingFaceInferenceAPI(
53
- model_name=model,
54
- token=api_key,
55
- task="text-generation",
56
- )
57
- Settings.llm = llm
58
- # maximum input size to the LLM
59
- Settings.context_window = context_window
60
-
61
- # number of tokens reserved for text generation.
62
- Settings.num_output = num_output
63
-
64
-
65
-
66
- def completion_wrapper(func: Callable) -> Callable:
67
- @wraps(func)
68
- def wrapper(
69
- prompt: str,
70
- system_message: str = "You are a helpful assistant.",
71
- temperature: float = 0.3,
72
- json_mode: bool = False,
73
- ) -> Union[str, dict]:
74
- """
75
- Generate a completion using the OpenAI API.
76
-
77
- Args:
78
- prompt (str): The user's prompt or query.
79
- system_message (str, optional): The system message to set the context for the assistant.
80
- Defaults to "You are a helpful assistant.".
81
- temperature (float, optional): The sampling temperature for controlling the randomness of the generated text.
82
- Defaults to 0.3.
83
- json_mode (bool, optional): Whether to return the response in JSON format.
84
- Defaults to False.
85
-
86
- Returns:
87
- Union[str, dict]: The generated completion.
88
- If json_mode is True, returns the complete API response as a dictionary.
89
- If json_mode is False, returns the generated text as a string.
90
- """
91
- llm = Settings.llm
92
- if llm.class_name() == "HuggingFaceInferenceAPI":
93
- llm.system_prompt = system_message
94
- messages = [
95
- ChatMessage(
96
- role="user", content=prompt),
97
- ]
98
- response = llm.chat(
99
- messages=messages,
100
- temperature=temperature,
101
- top_p=1,
102
- )
103
- return response.message.content
104
- else:
105
- messages = [
106
- ChatMessage(
107
- role="system", content=system_message),
108
- ChatMessage(
109
- role="user", content=prompt),
110
- ]
111
-
112
- if json_mode:
113
- response = llm.chat(
114
- temperature=temperature,
115
- top_p=1,
116
- response_format={"type": "json_object"},
117
- messages=messages,
118
- )
119
- return response.message.content
120
- else:
121
- response = llm.chat(
122
- temperature=temperature,
123
- top_p=1,
124
- messages=messages,
125
- )
126
- return response.message.content
127
-
128
- return wrapper
129
-
130
- openai_completion = get_completion
 
131
  get_completion = completion_wrapper(openai_completion)
 
1
+ # a monkey patch to use llama-index completion
2
+ import os
3
+ from typing import Union, Callable
4
+ from functools import wraps
5
+ from src.translation_agent.utils import *
6
+
7
+
8
+ from llama_index.llms.groq import Groq
9
+ from llama_index.llms.cohere import Cohere
10
+ from llama_index.llms.openai import OpenAI
11
+ from llama_index.llms.together import TogetherLLM
12
+ from llama_index.llms.ollama import Ollama
13
+ from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
14
+
15
+ from llama_index.core import Settings
16
+ from llama_index.core.llms import ChatMessage
17
+
18
+
19
+ # Add your LLMs here
20
+
21
+ def model_load(
22
+ endpoint: str,
23
+ model: str,
24
+ api_key: str = None,
25
+ context_window: int = 4096,
26
+ num_output: int = 512,
27
+ ):
28
+ if endpoint == "Groq":
29
+ llm = Groq(
30
+ model=model,
31
+ api_key=api_key,
32
+ )
33
+ elif endpoint == "Cohere":
34
+ llm = Cohere(
35
+ model=model,
36
+ api_key=api_key,
37
+ )
38
+ elif endpoint == "OpenAI":
39
+ llm = OpenAI(
40
+ model=model,
41
+ api_key=api_key if api_key else os.getenv("OPENAI_API_KEY"),
42
+ )
43
+ elif endpoint == "TogetherAI":
44
+ llm = TogetherLLM(
45
+ model=model,
46
+ api_key=api_key,
47
+ )
48
+ elif endpoint == "ollama":
49
+ llm = Ollama(
50
+ model=model,
51
+ request_timeout=120.0)
52
+ elif endpoint == "Huggingface":
53
+ llm = HuggingFaceInferenceAPI(
54
+ model_name=model,
55
+ token=api_key,
56
+ task="text-generation",
57
+ )
58
+ Settings.llm = llm
59
+ # maximum input size to the LLM
60
+ Settings.context_window = context_window
61
+
62
+ # number of tokens reserved for text generation.
63
+ Settings.num_output = num_output
64
+
65
+
66
+
67
+ def completion_wrapper(func: Callable) -> Callable:
68
+ @wraps(func)
69
+ def wrapper(
70
+ prompt: str,
71
+ system_message: str = "You are a helpful assistant.",
72
+ temperature: float = 0.3,
73
+ json_mode: bool = False,
74
+ ) -> Union[str, dict]:
75
+ """
76
+ Generate a completion using the OpenAI API.
77
+
78
+ Args:
79
+ prompt (str): The user's prompt or query.
80
+ system_message (str, optional): The system message to set the context for the assistant.
81
+ Defaults to "You are a helpful assistant.".
82
+ temperature (float, optional): The sampling temperature for controlling the randomness of the generated text.
83
+ Defaults to 0.3.
84
+ json_mode (bool, optional): Whether to return the response in JSON format.
85
+ Defaults to False.
86
+
87
+ Returns:
88
+ Union[str, dict]: The generated completion.
89
+ If json_mode is True, returns the complete API response as a dictionary.
90
+ If json_mode is False, returns the generated text as a string.
91
+ """
92
+ llm = Settings.llm
93
+ if llm.class_name() == "HuggingFaceInferenceAPI":
94
+ llm.system_prompt = system_message
95
+ messages = [
96
+ ChatMessage(
97
+ role="user", content=prompt),
98
+ ]
99
+ response = llm.chat(
100
+ messages=messages,
101
+ temperature=temperature,
102
+ top_p=1,
103
+ )
104
+ return response.message.content
105
+ else:
106
+ messages = [
107
+ ChatMessage(
108
+ role="system", content=system_message),
109
+ ChatMessage(
110
+ role="user", content=prompt),
111
+ ]
112
+
113
+ if json_mode:
114
+ response = llm.chat(
115
+ temperature=temperature,
116
+ top_p=1,
117
+ response_format={"type": "json_object"},
118
+ messages=messages,
119
+ )
120
+ return response.message.content
121
+ else:
122
+ response = llm.chat(
123
+ temperature=temperature,
124
+ top_p=1,
125
+ messages=messages,
126
+ )
127
+ return response.message.content
128
+
129
+ return wrapper
130
+
131
+ openai_completion = get_completion
132
  get_completion = completion_wrapper(openai_completion)
app/webui/process.py CHANGED
@@ -2,7 +2,7 @@ from polyglot.detect import Detector
2
  from polyglot.text import Text
3
  from difflib import Differ
4
  from icecream import ic
5
- from app.webui.patch import *
6
  from llama_index.core.node_parser import SentenceSplitter
7
 
8
  def lang_detector(text):
 
2
  from polyglot.text import Text
3
  from difflib import Differ
4
  from icecream import ic
5
+ from .patch import *
6
  from llama_index.core.node_parser import SentenceSplitter
7
 
8
  def lang_detector(text):