Spaces:
Running
Running
Update app/webui/app.py
Browse files- app/webui/app.py +189 -189
app/webui/app.py
CHANGED
@@ -1,190 +1,190 @@
|
|
1 |
-
import sys
|
2 |
-
import os
|
3 |
-
|
4 |
-
# Add the project root to the Python path
|
5 |
-
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
|
6 |
-
sys.path.insert(0, project_root)
|
7 |
-
|
8 |
-
import re
|
9 |
-
import gradio as gr
|
10 |
-
from app.webui.process import model_load, diff_texts, translator, translator_sec
|
11 |
-
from llama_index.core import SimpleDirectoryReader
|
12 |
-
|
13 |
-
def huanik(
|
14 |
-
endpoint,
|
15 |
-
model,
|
16 |
-
api_key,
|
17 |
-
choice,
|
18 |
-
endpoint2,
|
19 |
-
model2,
|
20 |
-
api_key2,
|
21 |
-
source_lang,
|
22 |
-
target_lang,
|
23 |
-
source_text,
|
24 |
-
country,
|
25 |
-
max_tokens,
|
26 |
-
context_window,
|
27 |
-
num_output,
|
28 |
-
):
|
29 |
-
|
30 |
-
if not source_text or source_lang == target_lang:
|
31 |
-
raise gr.Error("Please check that the content or options are entered correctly.")
|
32 |
-
|
33 |
-
try:
|
34 |
-
model_load(endpoint, model, api_key, context_window, num_output)
|
35 |
-
except Exception as e:
|
36 |
-
raise gr.Error(f"An unexpected error occurred: {e}")
|
37 |
-
|
38 |
-
source_text = re.sub(r'\n+', '\n', source_text)
|
39 |
-
|
40 |
-
if choice:
|
41 |
-
init_translation, reflect_translation, final_translation = translator_sec(
|
42 |
-
endpoint2=endpoint2,
|
43 |
-
model2=model2,
|
44 |
-
api_key2=api_key2,
|
45 |
-
context_window=context_window,
|
46 |
-
num_output=num_output,
|
47 |
-
source_lang=source_lang,
|
48 |
-
target_lang=target_lang,
|
49 |
-
source_text=source_text,
|
50 |
-
country=country,
|
51 |
-
max_tokens=max_tokens,
|
52 |
-
)
|
53 |
-
|
54 |
-
else:
|
55 |
-
init_translation, reflect_translation, final_translation = translator(
|
56 |
-
source_lang=source_lang,
|
57 |
-
target_lang=target_lang,
|
58 |
-
source_text=source_text,
|
59 |
-
country=country,
|
60 |
-
max_tokens=max_tokens,
|
61 |
-
)
|
62 |
-
|
63 |
-
final_diff = gr.HighlightedText(
|
64 |
-
diff_texts(init_translation, final_translation),
|
65 |
-
label="Diff translation",
|
66 |
-
combine_adjacent=True,
|
67 |
-
show_legend=True,
|
68 |
-
visible=True,
|
69 |
-
color_map={"removed": "red", "added": "green"})
|
70 |
-
|
71 |
-
return init_translation, reflect_translation, final_translation, final_diff
|
72 |
-
|
73 |
-
def update_model(endpoint):
|
74 |
-
endpoint_model_map = {
|
75 |
-
"Groq": "llama3-70b-8192",
|
76 |
-
"OpenAI": "gpt-4o",
|
77 |
-
"Cohere": "command-r",
|
78 |
-
"TogetherAI": "Qwen/Qwen2-72B-Instruct",
|
79 |
-
"Ollama": "llama3",
|
80 |
-
"Huggingface": "mistralai/Mistral-7B-Instruct-v0.3"
|
81 |
-
}
|
82 |
-
return gr.update(value=endpoint_model_map[endpoint])
|
83 |
-
|
84 |
-
def read_doc(file):
|
85 |
-
docs = SimpleDirectoryReader(input_files=[file]).load_data()
|
86 |
-
return docs[0].text
|
87 |
-
|
88 |
-
def enable_sec(choice):
|
89 |
-
if choice:
|
90 |
-
return gr.update(visible = True), gr.update(visible = True), gr.update(visible = True)
|
91 |
-
else:
|
92 |
-
return gr.update(visible = False), gr.update(visible = False), gr.update(visible = False)
|
93 |
-
|
94 |
-
|
95 |
-
TITLE = """
|
96 |
-
<h1><a href="https://github.com/andrewyng/translation-agent">Translation-Agent</a> webUI</h1>
|
97 |
-
"""
|
98 |
-
|
99 |
-
CSS = """
|
100 |
-
h1 {
|
101 |
-
text-align: center;
|
102 |
-
display: block;
|
103 |
-
height: 10vh;
|
104 |
-
align-content: center;
|
105 |
-
}
|
106 |
-
footer {
|
107 |
-
visibility: hidden;
|
108 |
-
}
|
109 |
-
"""
|
110 |
-
|
111 |
-
with gr.Blocks(theme="soft", css=CSS, fill_height=True) as demo:
|
112 |
-
gr.Markdown(TITLE)
|
113 |
-
with gr.Row():
|
114 |
-
with gr.Column(scale=1):
|
115 |
-
endpoint = gr.Dropdown(
|
116 |
-
label="Endpoint",
|
117 |
-
choices=["Groq","OpenAI","Cohere","TogetherAI","Ollama","Huggingface"],
|
118 |
-
value="
|
119 |
-
)
|
120 |
-
choice = gr.Checkbox(label="Second Endpoint", info="Add second endpoint for reflection")
|
121 |
-
model = gr.Textbox(label="Model", value="
|
122 |
-
api_key = gr.Textbox(label="API_KEY", type="password", )
|
123 |
-
endpoint2 = gr.Dropdown(
|
124 |
-
label="Endpoint 2",
|
125 |
-
choices=["Groq","OpenAI","Cohere","TogetherAI","Ollama","Huggingface"],
|
126 |
-
value="OpenAI",
|
127 |
-
visible=False,
|
128 |
-
)
|
129 |
-
model2 = gr.Textbox(label="Model 2", value="gpt-4o", visible=False, )
|
130 |
-
api_key2 = gr.Textbox(label="API_KEY 2", type="password", visible=False,)
|
131 |
-
source_lang = gr.Textbox(
|
132 |
-
label="Source Lang",
|
133 |
-
value="English",
|
134 |
-
)
|
135 |
-
target_lang = gr.Textbox(
|
136 |
-
label="Target Lang",
|
137 |
-
value="Spanish",
|
138 |
-
)
|
139 |
-
country = gr.Textbox(label="Country", value="Argentina", max_lines=1)
|
140 |
-
with gr.Accordion("Advanced Options", open=False):
|
141 |
-
max_tokens = gr.Slider(
|
142 |
-
label="Max tokens Per Chunk",
|
143 |
-
minimum=512,
|
144 |
-
maximum=2046,
|
145 |
-
value=1000,
|
146 |
-
step=8,
|
147 |
-
)
|
148 |
-
context_window = gr.Slider(
|
149 |
-
label="Context Window",
|
150 |
-
minimum=512,
|
151 |
-
maximum=8192,
|
152 |
-
value=4096,
|
153 |
-
step=8,
|
154 |
-
)
|
155 |
-
num_output = gr.Slider(
|
156 |
-
label="Output Num",
|
157 |
-
minimum=256,
|
158 |
-
maximum=8192,
|
159 |
-
value=512,
|
160 |
-
step=8,
|
161 |
-
)
|
162 |
-
with gr.Column(scale=4):
|
163 |
-
source_text = gr.Textbox(
|
164 |
-
label="Source Text",
|
165 |
-
value="How we live is so different from how we ought to live that he who studies "+\
|
166 |
-
"what ought to be done rather than what is done will learn the way to his downfall "+\
|
167 |
-
"rather than to his preservation.",
|
168 |
-
lines=10,
|
169 |
-
)
|
170 |
-
with gr.Tab("Final"):
|
171 |
-
output_final = gr.Textbox(label="FInal Translation", lines=10, show_copy_button=True)
|
172 |
-
with gr.Tab("Initial"):
|
173 |
-
output_init = gr.Textbox(label="Init Translation", lines=10, show_copy_button=True)
|
174 |
-
with gr.Tab("Reflection"):
|
175 |
-
output_reflect = gr.Textbox(label="Reflection", lines=10, show_copy_button=True)
|
176 |
-
with gr.Tab("Diff"):
|
177 |
-
output_diff = gr.HighlightedText(visible = False)
|
178 |
-
with gr.Row():
|
179 |
-
submit = gr.Button(value="Submit")
|
180 |
-
upload = gr.UploadButton(label="Upload", file_types=["text"])
|
181 |
-
clear = gr.ClearButton([source_text, output_init, output_reflect, output_final])
|
182 |
-
|
183 |
-
endpoint.change(fn=update_model, inputs=[endpoint], outputs=[model])
|
184 |
-
choice.select(fn=enable_sec, inputs=[choice], outputs=[endpoint2, model2, api_key2])
|
185 |
-
endpoint2.change(fn=update_model, inputs=[endpoint2], outputs=[model2])
|
186 |
-
submit.click(fn=huanik, inputs=[endpoint, model, api_key, choice, endpoint2, model2, api_key2, source_lang, target_lang, source_text, country, max_tokens, context_window, num_output], outputs=[output_init, output_reflect, output_final, output_diff])
|
187 |
-
upload.upload(fn=read_doc, inputs = upload, outputs = source_text)
|
188 |
-
|
189 |
-
if __name__ == "__main__":
|
190 |
demo.queue(api_open=False).launch(show_api=False, share=False)
|
|
|
1 |
+
import sys
|
2 |
+
import os
|
3 |
+
|
4 |
+
# Add the project root to the Python path
|
5 |
+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
|
6 |
+
sys.path.insert(0, project_root)
|
7 |
+
|
8 |
+
import re
|
9 |
+
import gradio as gr
|
10 |
+
from app.webui.process import model_load, diff_texts, translator, translator_sec
|
11 |
+
from llama_index.core import SimpleDirectoryReader
|
12 |
+
|
13 |
+
def huanik(
|
14 |
+
endpoint,
|
15 |
+
model,
|
16 |
+
api_key,
|
17 |
+
choice,
|
18 |
+
endpoint2,
|
19 |
+
model2,
|
20 |
+
api_key2,
|
21 |
+
source_lang,
|
22 |
+
target_lang,
|
23 |
+
source_text,
|
24 |
+
country,
|
25 |
+
max_tokens,
|
26 |
+
context_window,
|
27 |
+
num_output,
|
28 |
+
):
|
29 |
+
|
30 |
+
if not source_text or source_lang == target_lang:
|
31 |
+
raise gr.Error("Please check that the content or options are entered correctly.")
|
32 |
+
|
33 |
+
try:
|
34 |
+
model_load(endpoint, model, api_key, context_window, num_output)
|
35 |
+
except Exception as e:
|
36 |
+
raise gr.Error(f"An unexpected error occurred: {e}")
|
37 |
+
|
38 |
+
source_text = re.sub(r'\n+', '\n', source_text)
|
39 |
+
|
40 |
+
if choice:
|
41 |
+
init_translation, reflect_translation, final_translation = translator_sec(
|
42 |
+
endpoint2=endpoint2,
|
43 |
+
model2=model2,
|
44 |
+
api_key2=api_key2,
|
45 |
+
context_window=context_window,
|
46 |
+
num_output=num_output,
|
47 |
+
source_lang=source_lang,
|
48 |
+
target_lang=target_lang,
|
49 |
+
source_text=source_text,
|
50 |
+
country=country,
|
51 |
+
max_tokens=max_tokens,
|
52 |
+
)
|
53 |
+
|
54 |
+
else:
|
55 |
+
init_translation, reflect_translation, final_translation = translator(
|
56 |
+
source_lang=source_lang,
|
57 |
+
target_lang=target_lang,
|
58 |
+
source_text=source_text,
|
59 |
+
country=country,
|
60 |
+
max_tokens=max_tokens,
|
61 |
+
)
|
62 |
+
|
63 |
+
final_diff = gr.HighlightedText(
|
64 |
+
diff_texts(init_translation, final_translation),
|
65 |
+
label="Diff translation",
|
66 |
+
combine_adjacent=True,
|
67 |
+
show_legend=True,
|
68 |
+
visible=True,
|
69 |
+
color_map={"removed": "red", "added": "green"})
|
70 |
+
|
71 |
+
return init_translation, reflect_translation, final_translation, final_diff
|
72 |
+
|
73 |
+
def update_model(endpoint):
|
74 |
+
endpoint_model_map = {
|
75 |
+
"Groq": "llama3-70b-8192",
|
76 |
+
"OpenAI": "gpt-4o",
|
77 |
+
"Cohere": "command-r",
|
78 |
+
"TogetherAI": "Qwen/Qwen2-72B-Instruct",
|
79 |
+
"Ollama": "llama3",
|
80 |
+
"Huggingface": "mistralai/Mistral-7B-Instruct-v0.3"
|
81 |
+
}
|
82 |
+
return gr.update(value=endpoint_model_map[endpoint])
|
83 |
+
|
84 |
+
def read_doc(file):
|
85 |
+
docs = SimpleDirectoryReader(input_files=[file]).load_data()
|
86 |
+
return docs[0].text
|
87 |
+
|
88 |
+
def enable_sec(choice):
|
89 |
+
if choice:
|
90 |
+
return gr.update(visible = True), gr.update(visible = True), gr.update(visible = True)
|
91 |
+
else:
|
92 |
+
return gr.update(visible = False), gr.update(visible = False), gr.update(visible = False)
|
93 |
+
|
94 |
+
|
95 |
+
TITLE = """
|
96 |
+
<h1><a href="https://github.com/andrewyng/translation-agent">Translation-Agent</a> webUI</h1>
|
97 |
+
"""
|
98 |
+
|
99 |
+
CSS = """
|
100 |
+
h1 {
|
101 |
+
text-align: center;
|
102 |
+
display: block;
|
103 |
+
height: 10vh;
|
104 |
+
align-content: center;
|
105 |
+
}
|
106 |
+
footer {
|
107 |
+
visibility: hidden;
|
108 |
+
}
|
109 |
+
"""
|
110 |
+
|
111 |
+
with gr.Blocks(theme="soft", css=CSS, fill_height=True) as demo:
|
112 |
+
gr.Markdown(TITLE)
|
113 |
+
with gr.Row():
|
114 |
+
with gr.Column(scale=1):
|
115 |
+
endpoint = gr.Dropdown(
|
116 |
+
label="Endpoint",
|
117 |
+
choices=["Groq","OpenAI","Cohere","TogetherAI","Ollama","Huggingface"],
|
118 |
+
value="Groq",
|
119 |
+
)
|
120 |
+
choice = gr.Checkbox(label="Second Endpoint", info="Add second endpoint for reflection")
|
121 |
+
model = gr.Textbox(label="Model", value="llama3-70b-8192", )
|
122 |
+
api_key = gr.Textbox(label="API_KEY", type="password", )
|
123 |
+
endpoint2 = gr.Dropdown(
|
124 |
+
label="Endpoint 2",
|
125 |
+
choices=["Groq","OpenAI","Cohere","TogetherAI","Ollama","Huggingface"],
|
126 |
+
value="OpenAI",
|
127 |
+
visible=False,
|
128 |
+
)
|
129 |
+
model2 = gr.Textbox(label="Model 2", value="gpt-4o", visible=False, )
|
130 |
+
api_key2 = gr.Textbox(label="API_KEY 2", type="password", visible=False,)
|
131 |
+
source_lang = gr.Textbox(
|
132 |
+
label="Source Lang",
|
133 |
+
value="English",
|
134 |
+
)
|
135 |
+
target_lang = gr.Textbox(
|
136 |
+
label="Target Lang",
|
137 |
+
value="Spanish",
|
138 |
+
)
|
139 |
+
country = gr.Textbox(label="Country", value="Argentina", max_lines=1)
|
140 |
+
with gr.Accordion("Advanced Options", open=False):
|
141 |
+
max_tokens = gr.Slider(
|
142 |
+
label="Max tokens Per Chunk",
|
143 |
+
minimum=512,
|
144 |
+
maximum=2046,
|
145 |
+
value=1000,
|
146 |
+
step=8,
|
147 |
+
)
|
148 |
+
context_window = gr.Slider(
|
149 |
+
label="Context Window",
|
150 |
+
minimum=512,
|
151 |
+
maximum=8192,
|
152 |
+
value=4096,
|
153 |
+
step=8,
|
154 |
+
)
|
155 |
+
num_output = gr.Slider(
|
156 |
+
label="Output Num",
|
157 |
+
minimum=256,
|
158 |
+
maximum=8192,
|
159 |
+
value=512,
|
160 |
+
step=8,
|
161 |
+
)
|
162 |
+
with gr.Column(scale=4):
|
163 |
+
source_text = gr.Textbox(
|
164 |
+
label="Source Text",
|
165 |
+
value="How we live is so different from how we ought to live that he who studies "+\
|
166 |
+
"what ought to be done rather than what is done will learn the way to his downfall "+\
|
167 |
+
"rather than to his preservation.",
|
168 |
+
lines=10,
|
169 |
+
)
|
170 |
+
with gr.Tab("Final"):
|
171 |
+
output_final = gr.Textbox(label="FInal Translation", lines=10, show_copy_button=True)
|
172 |
+
with gr.Tab("Initial"):
|
173 |
+
output_init = gr.Textbox(label="Init Translation", lines=10, show_copy_button=True)
|
174 |
+
with gr.Tab("Reflection"):
|
175 |
+
output_reflect = gr.Textbox(label="Reflection", lines=10, show_copy_button=True)
|
176 |
+
with gr.Tab("Diff"):
|
177 |
+
output_diff = gr.HighlightedText(visible = False)
|
178 |
+
with gr.Row():
|
179 |
+
submit = gr.Button(value="Submit")
|
180 |
+
upload = gr.UploadButton(label="Upload", file_types=["text"])
|
181 |
+
clear = gr.ClearButton([source_text, output_init, output_reflect, output_final])
|
182 |
+
|
183 |
+
endpoint.change(fn=update_model, inputs=[endpoint], outputs=[model])
|
184 |
+
choice.select(fn=enable_sec, inputs=[choice], outputs=[endpoint2, model2, api_key2])
|
185 |
+
endpoint2.change(fn=update_model, inputs=[endpoint2], outputs=[model2])
|
186 |
+
submit.click(fn=huanik, inputs=[endpoint, model, api_key, choice, endpoint2, model2, api_key2, source_lang, target_lang, source_text, country, max_tokens, context_window, num_output], outputs=[output_init, output_reflect, output_final, output_diff])
|
187 |
+
upload.upload(fn=read_doc, inputs = upload, outputs = source_text)
|
188 |
+
|
189 |
+
if __name__ == "__main__":
|
190 |
demo.queue(api_open=False).launch(show_api=False, share=False)
|