Skip to content

Commit

Permalink
Add: Stop button to cancel translation progress, llm error notificati…
Browse files Browse the repository at this point in the history
…on, fix Readme
  • Loading branch information
snekkenull authored Jul 4, 2024
1 parent 99e3c8b commit 98bb86b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/webui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Llama Index supported, easily extendable

1. Select your desired translation API from the Endpoint dropdown menu.
2. Input the source language, target language, and country(optional).
3. If using Hugging Face API, enter your `HF_TOKEN` in the `api_key` textbox.
3. If using Hugging Face API, enter your `HF_TOKEN` in the `api_key` textbox, enter `MODEL_ID` or `HF_ENDPOINT_URL` in `Model` textbox.
4. Input the source text or upload your document file.
5. Submit and get translation, the UI will display the translated text with tokenization and highlight differences.
6. Enable Second Endpoint, you can add another endpoint by different LLMs for reflection.
Expand Down
42 changes: 29 additions & 13 deletions app/webui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def read_doc(file):

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

def update_menu(visible):
return not visible, gr.update(visible=not visible)
Expand All @@ -114,6 +114,13 @@ def switch(source_lang,source_text,target_lang,output_final):
else:
return gr.update(value=target_lang), gr.update(value=source_text), gr.update(value=source_lang), gr.update(value="")

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

def closeBtnHide(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 @@ -191,17 +198,17 @@ def switch(source_lang,source_text,target_lang,output_final):
choices=["Groq","OpenAI","Cohere","TogetherAI","Ollama","Huggingface"],
value="OpenAI",
)
choice = gr.Checkbox(label="Second Endpoint", info="Add second endpoint for reflection")
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", )
endpoint2 = gr.Dropdown(
label="Endpoint 2",
choices=["Groq","OpenAI","Cohere","TogetherAI","Ollama","Huggingface"],
value="OpenAI",
visible=False,
)
model2 = gr.Textbox(label="Model 2", value="gpt-4o", visible=False,)
api_key2 = gr.Textbox(label="API_KEY 2", type="password", visible=False,)
with gr.Column(visible=False) as AddEndpoint:
endpoint2 = gr.Dropdown(
label="Additional Endpoint",
choices=["Groq","OpenAI","Cohere","TogetherAI","Ollama","Huggingface"],
value="OpenAI",
)
model2 = gr.Textbox(label="Model", value="gpt-4o", )
api_key2 = gr.Textbox(label="API_KEY", type="password", )
with gr.Row():
source_lang = gr.Textbox(
label="Source Lang",
Expand Down Expand Up @@ -265,14 +272,23 @@ def switch(source_lang,source_text,target_lang,output_final):
upload = gr.UploadButton(label="Upload", file_types=["text"])
export = gr.DownloadButton(visible=False)
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])

menuBtn.click(fn=update_menu, inputs=visible, outputs=[visible, menubar], js=JS)
endpoint.change(fn=update_model, inputs=[endpoint], outputs=[model])
choice.select(fn=enable_sec, inputs=[choice], outputs=[endpoint2, model2, api_key2])

choice.select(fn=enable_sec, inputs=[choice], outputs=[AddEndpoint])
endpoint2.change(fn=update_model, inputs=[endpoint2], outputs=[model2])
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, rpm], outputs=[output_init, output_reflect, output_final, output_diff])

start_ta = 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, 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])
close.click(fn=None, cancels=start_ta)

if __name__ == "__main__":
demo.queue(api_open=False).launch(show_api=False, share=False)
27 changes: 16 additions & 11 deletions app/webui/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ def get_completion(
ChatMessage(
role="user", content=prompt),
]

response = llm.chat(
messages=messages,
temperature=temperature,
)
return response.message.content
try:
response = llm.chat(
messages=messages,
temperature=temperature,
)
return response.message.content
except Exception as e:
raise gr.Error(f"An unexpected error occurred: {e}")
else:
messages = [
ChatMessage(
Expand All @@ -143,11 +145,14 @@ def get_completion(
)
return response.message.content
else:
response = llm.chat(
temperature=temperature,
messages=messages,
)
return response.message.content
try:
response = llm.chat(
temperature=temperature,
messages=messages,
)
return response.message.content
except Exception as e:
raise gr.Error(f"An unexpected error occurred: {e}")

utils.get_completion = get_completion

Expand Down

0 comments on commit 98bb86b

Please sign in to comment.