vilarin commited on
Commit
73db053
1 Parent(s): 584fdfd

Update app/webui/app.py

Browse files
Files changed (1) hide show
  1. app/webui/app.py +146 -146
app/webui/app.py CHANGED
@@ -1,147 +1,147 @@
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(
7
- endpoint,
8
- model,
9
- api_key,
10
- source_lang,
11
- target_lang,
12
- source_text,
13
- country,
14
- max_tokens,
15
- context_window,
16
- num_output,
17
- ):
18
-
19
- if not source_text or source_lang == target_lang:
20
- raise gr.Error("Please check that the content or options are entered correctly.")
21
-
22
- try:
23
- model_load(endpoint, model, api_key, context_window, num_output)
24
- except Exception as e:
25
- raise gr.Error(f"An unexpected error occurred: {e}")
26
-
27
- source_text = re.sub(r'\n+', '\n', source_text)
28
-
29
- init_translation, reflect_translation, final_translation = translator(
30
- source_lang=source_lang,
31
- target_lang=target_lang,
32
- source_text=source_text,
33
- country=country,
34
- max_tokens=max_tokens,
35
- )
36
-
37
- final_diff = gr.HighlightedText(
38
- diff_texts(init_translation, final_translation),
39
- label="Diff translation",
40
- combine_adjacent=True,
41
- show_legend=True,
42
- visible=True,
43
- color_map={"removed": "red", "added": "green"})
44
-
45
- return init_translation, reflect_translation, final_translation, final_diff
46
-
47
- def update_model(endpoint):
48
- endpoint_model_map = {
49
- "Groq": "llama3-70b-8192",
50
- "OpenAI": "gpt-4o",
51
- "Cohere": "command-r",
52
- "TogetherAI": "Qwen/Qwen2-72B-Instruct",
53
- "Ollama": "llama3",
54
- "Huggingface": "mistralai/Mistral-7B-Instruct-v0.3"
55
- }
56
- return gr.update(value=endpoint_model_map[endpoint])
57
-
58
- def read_doc(file):
59
- docs = SimpleDirectoryReader(input_files=file).load_data()
60
- return docs
61
-
62
- TITLE = """
63
- <h1><a href="https://github.com/andrewyng/translation-agent">Translation-Agent</a> webUI</h1>
64
- """
65
-
66
- CSS = """
67
- h1 {
68
- text-align: center;
69
- display: block;
70
- height: 10vh;
71
- align-content: center;
72
- }
73
- footer {
74
- visibility: hidden;
75
- }
76
- """
77
-
78
- with gr.Blocks(theme="soft", css=CSS) as demo:
79
- gr.Markdown(TITLE)
80
- with gr.Row():
81
- with gr.Column(scale=1):
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)",
91
- value="English",
92
- )
93
- target_lang = gr.Textbox(
94
- label="Target Lang",
95
- value="Spanish",
96
- )
97
- country = gr.Textbox(label="Country", value="Argentina", max_lines=1)
98
- with gr.Accordion("Advanced Options", open=False):
99
- max_tokens = gr.Slider(
100
- label="Max tokens Per Chunk",
101
- minimum=512,
102
- maximum=2046,
103
- value=1000,
104
- step=8,
105
- )
106
- context_window = gr.Slider(
107
- label="Context Window",
108
- minimum=512,
109
- maximum=8192,
110
- value=4096,
111
- step=8,
112
- )
113
- num_output = gr.Slider(
114
- label="Output Num",
115
- minimum=256,
116
- maximum=8192,
117
- value=512,
118
- step=8,
119
- )
120
- with gr.Column(scale=4):
121
- source_text = gr.Textbox(
122
- label="Source Text",
123
- value="How we live is so different from how we ought to live that he who studies "+\
124
- "what ought to be done rather than what is done will learn the way to his downfall "+\
125
- "rather than to his preservation.",
126
- lines=5,
127
- )
128
- with gr.Tab("Final"):
129
- output_final = gr.Textbox(label="FInal Translation", lines=3, show_copy_button=True)
130
- with gr.Tab("Initial"):
131
- output_init = gr.Textbox(label="Init Translation", lines=3, show_copy_button=True)
132
- with gr.Tab("Reflection"):
133
- output_reflect = gr.Textbox(label="Reflection", lines=3, show_copy_button=True)
134
- with gr.Tab("Diff"):
135
- output_diff = gr.HighlightedText(visible = False)
136
- with gr.Row():
137
- submit = gr.Button(value="Submit")
138
- upload = gr.UploadButton(label="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])
142
- source_text.change(lang_detector, source_text, source_lang)
143
- submit.click(fn=huanik, inputs=[endpoint, model, api_key, source_lang, target_lang, source_text, country, max_tokens, context_window, num_output], outputs=[output_init, output_reflect, output_final, output_diff])
144
- upload.upload(fn=read_doc, inputs = upload, outputs = source_text)
145
-
146
- if __name__ == "__main__":
147
  demo.queue(api_open=False).launch(show_api=False, share=False)
 
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(
7
+ endpoint,
8
+ model,
9
+ api_key,
10
+ source_lang,
11
+ target_lang,
12
+ source_text,
13
+ country,
14
+ max_tokens,
15
+ context_window,
16
+ num_output,
17
+ ):
18
+
19
+ if not source_text or source_lang == target_lang:
20
+ raise gr.Error("Please check that the content or options are entered correctly.")
21
+
22
+ try:
23
+ model_load(endpoint, model, api_key, context_window, num_output)
24
+ except Exception as e:
25
+ raise gr.Error(f"An unexpected error occurred: {e}")
26
+
27
+ source_text = re.sub(r'\n+', '\n', source_text)
28
+
29
+ init_translation, reflect_translation, final_translation = translator(
30
+ source_lang=source_lang,
31
+ target_lang=target_lang,
32
+ source_text=source_text,
33
+ country=country,
34
+ max_tokens=max_tokens,
35
+ )
36
+
37
+ final_diff = gr.HighlightedText(
38
+ diff_texts(init_translation, final_translation),
39
+ label="Diff translation",
40
+ combine_adjacent=True,
41
+ show_legend=True,
42
+ visible=True,
43
+ color_map={"removed": "red", "added": "green"})
44
+
45
+ return init_translation, reflect_translation, final_translation, final_diff
46
+
47
+ def update_model(endpoint):
48
+ endpoint_model_map = {
49
+ "Groq": "llama3-70b-8192",
50
+ "OpenAI": "gpt-4o",
51
+ "Cohere": "command-r",
52
+ "TogetherAI": "Qwen/Qwen2-72B-Instruct",
53
+ "Ollama": "llama3",
54
+ "Huggingface": "mistralai/Mistral-7B-Instruct-v0.3"
55
+ }
56
+ return gr.update(value=endpoint_model_map[endpoint])
57
+
58
+ def read_doc(file):
59
+ docs = SimpleDirectoryReader(input_files=file).load_data()
60
+ return docs
61
+
62
+ TITLE = """
63
+ <h1><a href="https://github.com/andrewyng/translation-agent">Translation-Agent</a> webUI</h1>
64
+ """
65
+
66
+ CSS = """
67
+ h1 {
68
+ text-align: center;
69
+ display: block;
70
+ height: 10vh;
71
+ align-content: center;
72
+ }
73
+ footer {
74
+ visibility: hidden;
75
+ }
76
+ """
77
+
78
+ with gr.Blocks(theme="soft", css=CSS) as demo:
79
+ gr.Markdown(TITLE)
80
+ with gr.Row():
81
+ with gr.Column(scale=1):
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)",
91
+ value="English",
92
+ )
93
+ target_lang = gr.Textbox(
94
+ label="Target Lang",
95
+ value="Spanish",
96
+ )
97
+ country = gr.Textbox(label="Country", value="Argentina", max_lines=1)
98
+ with gr.Accordion("Advanced Options", open=False):
99
+ max_tokens = gr.Slider(
100
+ label="Max tokens Per Chunk",
101
+ minimum=512,
102
+ maximum=2046,
103
+ value=1000,
104
+ step=8,
105
+ )
106
+ context_window = gr.Slider(
107
+ label="Context Window",
108
+ minimum=512,
109
+ maximum=8192,
110
+ value=4096,
111
+ step=8,
112
+ )
113
+ num_output = gr.Slider(
114
+ label="Output Num",
115
+ minimum=256,
116
+ maximum=8192,
117
+ value=512,
118
+ step=8,
119
+ )
120
+ with gr.Column(scale=4):
121
+ source_text = gr.Textbox(
122
+ label="Source Text",
123
+ value="How we live is so different from how we ought to live that he who studies "+\
124
+ "what ought to be done rather than what is done will learn the way to his downfall "+\
125
+ "rather than to his preservation.",
126
+ lines=5,
127
+ )
128
+ with gr.Tab("Final"):
129
+ output_final = gr.Textbox(label="FInal Translation", lines=3, show_copy_button=True)
130
+ with gr.Tab("Initial"):
131
+ output_init = gr.Textbox(label="Init Translation", lines=3, show_copy_button=True)
132
+ with gr.Tab("Reflection"):
133
+ output_reflect = gr.Textbox(label="Reflection", lines=3, show_copy_button=True)
134
+ with gr.Tab("Diff"):
135
+ output_diff = gr.HighlightedText(visible = False)
136
+ with gr.Row():
137
+ submit = gr.Button(value="Submit")
138
+ upload = gr.UploadButton(label="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])
142
+ source_text.change(lang_detector, source_text, source_lang)
143
+ submit.click(fn=huanik, inputs=[endpoint, model, api_key, source_lang, target_lang, source_text, country, max_tokens, context_window, num_output], outputs=[output_init, output_reflect, output_final, output_diff])
144
+ upload.upload(fn=read_doc, inputs = upload, outputs = source_text)
145
+
146
+ if __name__ == "__main__":
147
  demo.queue(api_open=False).launch(show_api=False, share=False)