Skip to content

Commit

Permalink
style and formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dominguez9 committed Jul 8, 2024
1 parent 0337371 commit ad40069
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 143 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ poetry.lock
floresp-v2.0-rc.3
*cache
wmt
outputs
6 changes: 3 additions & 3 deletions app/webui/README.md → app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ This repository contains a Gradio web UI for a translation agent that utilizes v
poetry install --with app
poetry shell
```

2. **Set API Keys:**
- Rename `.env.sample` to `.env`, you can add your API keys for each service:

Expand All @@ -53,11 +53,11 @@ This repository contains a Gradio web UI for a translation agent that utilizes v

**Linux**
```bash
python app/webui/app.py
python app/app.py
```
**Windows**
```bash
python .\app\webui\app.py
python .\app\app.py
```

4. **Access the Web UI:**
Expand Down
216 changes: 154 additions & 62 deletions app/webui/app.py → app/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import os
import re
import gradio as gr
from glob import glob
from process import model_load, diff_texts, translator, translator_sec, extract_docx, extract_pdf, extract_text

import gradio as gr
from process import diff_texts
from process import extract_docx
from process import extract_pdf
from process import extract_text
from process import model_load
from process import translator
from process import translator_sec


def huanik(
endpoint: str,
Expand All @@ -22,28 +30,31 @@ def huanik(
temperature: int,
rpm: int,
):

if not source_text or source_lang == target_lang:
raise gr.Error("Please check that the content or options are entered correctly.")
raise gr.Error(
"Please check that the content or options are entered correctly."
)

try:
model_load(endpoint, base, model, api_key, temperature, rpm)
except Exception as e:
raise gr.Error(f"An unexpected error occurred: {e}")
raise gr.Error(f"An unexpected error occurred: {e}") from e

source_text = re.sub(r'(?m)^\s*$\n?', '', source_text)
source_text = re.sub(r"(?m)^\s*$\n?", "", source_text)

if choice:
init_translation, reflect_translation, final_translation = translator_sec(
endpoint2=endpoint2,
base2=base2,
model2=model2,
api_key2=api_key2,
source_lang=source_lang,
target_lang=target_lang,
source_text=source_text,
country=country,
max_tokens=max_tokens,
init_translation, reflect_translation, final_translation = (
translator_sec(
endpoint2=endpoint2,
base2=base2,
model2=model2,
api_key2=api_key2,
source_lang=source_lang,
target_lang=target_lang,
source_text=source_text,
country=country,
max_tokens=max_tokens,
)
)

else:
Expand All @@ -61,10 +72,12 @@ def huanik(
combine_adjacent=True,
show_legend=True,
visible=True,
color_map={"removed": "red", "added": "green"})
color_map={"removed": "red", "added": "green"},
)

return init_translation, reflect_translation, final_translation, final_diff


def update_model(endpoint):
endpoint_model_map = {
"Groq": "llama3-70b-8192",
Expand All @@ -79,6 +92,7 @@ def update_model(endpoint):
base = gr.update(visible=False)
return gr.update(value=endpoint_model_map[endpoint]), base


def read_doc(path):
file_type = path.split(".")[-1]
print(file_type)
Expand All @@ -89,19 +103,22 @@ def read_doc(path):
content = extract_docx(path)
else:
content = extract_text(path)
return re.sub(r'(?m)^\s*$\n?', '', content)
return re.sub(r"(?m)^\s*$\n?", "", content)
else:
raise gr.Error("Oops, unsupported files.")


def enable_sec(choice):
if choice:
return gr.update(visible = True)
return gr.update(visible=True)
else:
return gr.update(visible = False)
return gr.update(visible=False)


def update_menu(visible):
return not visible, gr.update(visible=not visible)


def export_txt(strings):
os.makedirs("outputs", exist_ok=True)
base_count = len(glob(os.path.join("outputs", "*.txt")))
Expand All @@ -110,19 +127,33 @@ def export_txt(strings):
f.write(strings)
return gr.update(value=file_path, visible=True)

def switch(source_lang,source_text,target_lang,output_final):

def switch(source_lang, source_text, target_lang, output_final):
if output_final:
return gr.update(value=target_lang), gr.update(value=output_final), gr.update(value=source_lang), gr.update(value=source_text)
return (
gr.update(value=target_lang),
gr.update(value=output_final),
gr.update(value=source_lang),
gr.update(value=source_text),
)
else:
return gr.update(value=target_lang), gr.update(value=source_text), gr.update(value=source_lang), gr.update(value="")
return (
gr.update(value=target_lang),
gr.update(value=source_text),
gr.update(value=source_lang),
gr.update(value=""),
)


def closeBtnShow():
def close_btn_show():
return gr.update(visible=False), gr.update(visible=True)

def closeBtnHide(output_final):

def close_btn_hide(output_final):
if output_final:
return gr.update(visible=True), gr.update(visible=False)


TITLE = """
<div style="display: inline-flex;">
<div style="margin-left: 6px; font-size:32px; color: #6366f1"><b>Translation Agent</b> WebUI</div>
Expand Down Expand Up @@ -182,114 +213,175 @@ def closeBtnHide(output_final):

JS = """
function () {
const menuBtn = document.getElementById('menu');
menuBtn.classList.toggle('active');
const menu_btn = document.getElementById('menu');
menu_btn.classList.toggle('active');
}
"""

with gr.Blocks(theme="soft", css=CSS, fill_height=True) as demo:
with gr.Row():
visible = gr.State(value=True)
menuBtn = gr.Button(value="", elem_classes="menu_btn", elem_id="menu", size="sm")
menu_btn = gr.Button(
value="", elem_classes="menu_btn", elem_id="menu", size="sm"
)
gr.HTML(TITLE)
with gr.Row():
with gr.Column(scale=1) as menubar:
endpoint = gr.Dropdown(
label="Endpoint",
choices=["OpenAI","Groq","TogetherAI","Ollama","CUSTOM"],
choices=["OpenAI", "Groq", "TogetherAI", "Ollama", "CUSTOM"],
value="OpenAI",
)
choice = gr.Checkbox(label="Additional Endpoint", info="Additional endpoint for reflection")
model = gr.Textbox(label="Model", value="gpt-4o", )
api_key = gr.Textbox(label="API_KEY", type="password", )
choice = gr.Checkbox(
label="Additional Endpoint",
info="Additional endpoint for reflection",
)
model = gr.Textbox(
label="Model",
value="gpt-4o",
)
api_key = gr.Textbox(
label="API_KEY",
type="password",
)
base = gr.Textbox(label="BASE URL", visible=False)
with gr.Column(visible=False) as AddEndpoint:
endpoint2 = gr.Dropdown(
label="Additional Endpoint",
choices=["OpenAI","Groq","TogetherAI","Ollama","CUSTOM"],
choices=[
"OpenAI",
"Groq",
"TogetherAI",
"Ollama",
"CUSTOM",
],
value="OpenAI",
)
model2 = gr.Textbox(label="Model", value="gpt-4o", )
api_key2 = gr.Textbox(label="API_KEY", type="password", )
model2 = gr.Textbox(
label="Model",
value="gpt-4o",
)
api_key2 = gr.Textbox(
label="API_KEY",
type="password",
)
base2 = gr.Textbox(label="BASE URL", visible=False)
with gr.Row():
source_lang = gr.Textbox(
label="Source Lang",
value="English",
elem_classes = "lang",
elem_classes="lang",
)
target_lang = gr.Textbox(
label="Target Lang",
value="Spanish",
elem_classes = "lang",
elem_classes="lang",
)
switchBtn = gr.Button(value="🔄️")
country = gr.Textbox(label="Country", value="Argentina", max_lines=1)
switch_btn = gr.Button(value="🔄️")
country = gr.Textbox(
label="Country", value="Argentina", max_lines=1
)
with gr.Accordion("Advanced Options", open=False):
max_tokens = gr.Slider(
label="Max tokens Per Chunk",
minimum=512,
maximum=2046,
value=1000,
step=8,
)
)
temperature = gr.Slider(
label="Temperature",
minimum=0,
maximum=1.0,
value=0.3,
step=0.1,
)
)
rpm = gr.Slider(
label="Request Per Minute",
minimum=1,
maximum=1000,
value=60,
step=1,
)
# json_mode = gr.Checkbox(
# False,
# label="Json Mode",
# )
)

with gr.Column(scale=4):
source_text = gr.Textbox(
label="Source Text",
value="How we live is so different from how we ought to live that he who studies "+\
"what ought to be done rather than what is done will learn the way to his downfall "+\
"rather than to his preservation.",
value="How we live is so different from how we ought to live that he who studies "
+ "what ought to be done rather than what is done will learn the way to his downfall "
+ "rather than to his preservation.",
lines=12,
)
with gr.Tab("Final"):
output_final = gr.Textbox(label="FInal Translation", lines=12, show_copy_button=True)
output_final = gr.Textbox(
label="Final Translation", lines=12, show_copy_button=True
)
with gr.Tab("Initial"):
output_init = gr.Textbox(label="Init Translation", lines=12, show_copy_button=True)
output_init = gr.Textbox(
label="Init Translation", lines=12, show_copy_button=True
)
with gr.Tab("Reflection"):
output_reflect = gr.Textbox(label="Reflection", lines=12, show_copy_button=True)
output_reflect = gr.Textbox(
label="Reflection", lines=12, show_copy_button=True
)
with gr.Tab("Diff"):
output_diff = gr.HighlightedText(visible = False)
output_diff = gr.HighlightedText(visible=False)
with gr.Row():
submit = gr.Button(value="Translate")
upload = gr.UploadButton(label="Upload", file_types=["text"])
export = gr.DownloadButton(visible=False)
clear = gr.ClearButton([source_text, output_init, output_reflect, output_final])
clear = gr.ClearButton(
[source_text, output_init, output_reflect, output_final]
)
close = gr.Button(value="Stop", visible=False)

switchBtn.click(fn=switch, inputs=[source_lang,source_text,target_lang,output_final], outputs=[source_lang,source_text,target_lang,output_final])
switch_btn.click(
fn=switch,
inputs=[source_lang, source_text, target_lang, output_final],
outputs=[source_lang, source_text, target_lang, output_final],
)

menuBtn.click(fn=update_menu, inputs=visible, outputs=[visible, menubar], js=JS)
menu_btn.click(
fn=update_menu, inputs=visible, outputs=[visible, menubar], js=JS
)
endpoint.change(fn=update_model, inputs=[endpoint], outputs=[model, base])

choice.select(fn=enable_sec, inputs=[choice], outputs=[AddEndpoint])
endpoint2.change(fn=update_model, inputs=[endpoint2], outputs=[model2, base2])
endpoint2.change(
fn=update_model, inputs=[endpoint2], outputs=[model2, base2]
)

start_ta = submit.click(fn=huanik, inputs=[endpoint, base, model, api_key, choice, endpoint2, base2, model2, api_key2, source_lang, target_lang, source_text, country, max_tokens, temperature, rpm], outputs=[output_init, output_reflect, output_final, output_diff])
upload.upload(fn=read_doc, inputs = upload, outputs = source_text)
start_ta = submit.click(
fn=huanik,
inputs=[
endpoint,
base,
model,
api_key,
choice,
endpoint2,
base2,
model2,
api_key2,
source_lang,
target_lang,
source_text,
country,
max_tokens,
temperature,
rpm,
],
outputs=[output_init, output_reflect, output_final, output_diff],
)
upload.upload(fn=read_doc, inputs=upload, outputs=source_text)
output_final.change(fn=export_txt, inputs=output_final, outputs=[export])

submit.click(fn=closeBtnShow, outputs=[clear, close])
output_final.change(fn=closeBtnHide, inputs=output_final, outputs=[clear, close])
submit.click(fn=close_btn_show, outputs=[clear, close])
output_final.change(
fn=close_btn_hide, inputs=output_final, outputs=[clear, close]
)
close.click(fn=None, cancels=start_ta)

if __name__ == "__main__":
demo.queue(api_open=False).launch(show_api=False, share=False)
demo.queue(api_open=False).launch(show_api=False, share=False)
File renamed without changes
Loading

0 comments on commit ad40069

Please sign in to comment.