Upload 4 files
Browse files- app/webui/__init__.py +0 -0
- app/webui/app.py +153 -146
- 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
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
"
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
label="
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
label="
|
| 108 |
-
minimum=512,
|
| 109 |
-
maximum=
|
| 110 |
-
value=
|
| 111 |
-
step=8,
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
label="
|
| 115 |
-
minimum=
|
| 116 |
-
maximum=8192,
|
| 117 |
-
value=
|
| 118 |
-
step=8,
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|