Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from openai import OpenAI
|
4 |
+
|
5 |
+
client=OpenAI(base_url=os.getenv('OPENAI_BASE_URL'),api_key=os.getenv('OPENAI_API_KEY'))
|
6 |
+
|
7 |
+
def explain(input: str, lang: str):
|
8 |
+
prompt=f'as a language expert, explain how to express "{input}" in {lang}, answer in plain text concisely, if has some other expressions or examples can be improved understand, append it after a separator "-----".'
|
9 |
+
res=client.chat.completions.create(
|
10 |
+
model="gpt-4o",
|
11 |
+
messages=[{"role":"user","content":prompt}],
|
12 |
+
)
|
13 |
+
print(res)
|
14 |
+
return res.choices[0].message.content
|
15 |
+
|
16 |
+
_allow_langs=["English", "Chinese", "Japanese"]
|
17 |
+
|
18 |
+
with gr.Blocks() as demo:
|
19 |
+
gr.Markdown('# concept explain')
|
20 |
+
|
21 |
+
with gr.Row():
|
22 |
+
with gr.Column():
|
23 |
+
explain_input=gr.Textbox(label="Concept")
|
24 |
+
explain_lang=gr.Dropdown(label="Language", choices=_allow_langs, value=_allow_langs[0])
|
25 |
+
explain_btn=gr.Button("Run", variant="primary")
|
26 |
+
with gr.Column():
|
27 |
+
explain_result=gr.Textbox(label="Result")
|
28 |
+
|
29 |
+
explain_btn.click(fn=explain, inputs=[explain_input, explain_lang], outputs=[explain_result])
|
30 |
+
|
31 |
+
if __name__=="__main__":
|
32 |
+
demo.launch()
|