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

Upload 4 files

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