Mel Seto
commited on
Commit
·
75a7651
1
Parent(s):
4fa4f0a
add a mock inference client call for UI testing
Browse files
app.py
CHANGED
|
@@ -1,10 +1,36 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
-
import gradio as gr
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def generate_idiom(situation: str, client):
|
| 9 |
prompt = f"""
|
| 10 |
You are a wise assistant. Given a situation, respond with exactly:
|
|
@@ -20,90 +46,58 @@ Explanation
|
|
| 20 |
Situation: {situation}
|
| 21 |
Answer:
|
| 22 |
"""
|
| 23 |
-
# Use Cerebras chat completion API
|
| 24 |
response = client.chat.completions.create(
|
| 25 |
messages=[{"role": "user", "content": prompt}],
|
| 26 |
max_tokens=150
|
| 27 |
)
|
| 28 |
-
|
| 29 |
generated_text = response.choices[0].message.content.strip()
|
| 30 |
|
| 31 |
-
# Split lines for clean UI
|
| 32 |
lines = [line.strip() for line in generated_text.split("\n") if line.strip()]
|
| 33 |
if len(lines) >= 3:
|
| 34 |
idiom = lines[0]
|
| 35 |
pinyin = lines[1]
|
| 36 |
meaning = " ".join(lines[2:])
|
| 37 |
-
explanation = f"{pinyin}
|
| 38 |
else:
|
| 39 |
idiom = generated_text
|
| 40 |
explanation = ""
|
| 41 |
-
|
| 42 |
return idiom, explanation
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def launch_app():
|
| 46 |
-
|
| 47 |
-
client = InferenceClient(
|
| 48 |
-
provider="cerebras",
|
| 49 |
-
model="meta-llama/Llama-3.3-70B-Instruct",
|
| 50 |
-
api_key=os.environ["HF_TOKEN"]
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
with gr.Blocks(css="""
|
| 54 |
-
.idiom-output {
|
| 55 |
-
font-size: 2rem;
|
| 56 |
-
font-weight: bold;
|
| 57 |
-
text-align: center;
|
| 58 |
-
color: #8B0000;
|
| 59 |
-
margin-bottom: 0.5em;
|
| 60 |
-
}
|
| 61 |
-
.explanation-output {
|
| 62 |
-
font-size: 1rem;
|
| 63 |
-
line-height: 1.5;
|
| 64 |
-
color: #333333;
|
| 65 |
-
text-align: center;
|
| 66 |
-
}
|
| 67 |
-
.gradio-container {
|
| 68 |
-
background-color: #fdfcf7;
|
| 69 |
-
}
|
| 70 |
-
""") as demo:
|
| 71 |
-
|
| 72 |
gr.Markdown("## 🀄 Chinese Wisdom Generator\nEnter a situation, get a Chinese idiom with explanation.")
|
| 73 |
-
|
| 74 |
with gr.Row():
|
| 75 |
-
# Left column: input + examples + button
|
| 76 |
with gr.Column(scale=1):
|
| 77 |
-
|
| 78 |
label="Describe your situation...",
|
| 79 |
placeholder="e.g. I procrastinated on my homework again...",
|
| 80 |
lines=3
|
| 81 |
)
|
| 82 |
submit_btn = gr.Button("✨ Find Idiom")
|
| 83 |
-
|
| 84 |
gr.Examples(
|
| 85 |
examples=[
|
| 86 |
["I studied hard but still failed my exam."],
|
| 87 |
["I missed my bus because I woke up late."],
|
| 88 |
["I finally finished a long project after months."],
|
| 89 |
],
|
| 90 |
-
inputs=[
|
| 91 |
)
|
| 92 |
-
|
| 93 |
-
# Right column: outputs
|
| 94 |
with gr.Column(scale=1):
|
| 95 |
idiom_output = gr.HTML("<div class='idiom-output'>—</div>")
|
| 96 |
explanation_output = gr.HTML("<div class='explanation-output'>—</div>")
|
| 97 |
|
| 98 |
-
|
| 99 |
-
def update_ui(situation):
|
| 100 |
-
idiom, explanation = generate_idiom(situation, client)
|
| 101 |
-
return f"<div class='idiom-output'>{idiom}</div>", f"<div class='explanation-output'>{explanation}</div>"
|
| 102 |
-
|
| 103 |
-
submit_btn.click(update_ui, inputs=[situation_input], outputs=[idiom_output, explanation_output])
|
| 104 |
-
|
| 105 |
demo.launch(debug=True)
|
| 106 |
|
| 107 |
-
|
| 108 |
if __name__ == "__main__":
|
| 109 |
launch_app()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import os
|
| 3 |
from dotenv import load_dotenv
|
|
|
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
+
# ======================
|
| 9 |
+
# Toggle between Mock / Real API
|
| 10 |
+
# ======================
|
| 11 |
+
USE_MOCK = True # Set False to use the real Cerebras API
|
| 12 |
+
|
| 13 |
+
# ======================
|
| 14 |
+
# Instantiate client (only if not using mock)
|
| 15 |
+
# ======================
|
| 16 |
+
if not USE_MOCK:
|
| 17 |
+
client = InferenceClient(
|
| 18 |
+
provider="cerebras",
|
| 19 |
+
model="cerebras/btlm-3b-8k-base",
|
| 20 |
+
api_key=os.environ["HF_TOKEN"]
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# ======================
|
| 24 |
+
# Mock function for testing UI
|
| 25 |
+
# ======================
|
| 26 |
+
def generate_idiom_mock(situation: str):
|
| 27 |
+
idiom = "对症下药"
|
| 28 |
+
explanation = "duì zhèng xià yào<br><br>To prescribe the right medicine; to take the right approach to a problem."
|
| 29 |
+
return idiom, explanation
|
| 30 |
+
|
| 31 |
+
# ======================
|
| 32 |
+
# Real API function
|
| 33 |
+
# ======================
|
| 34 |
def generate_idiom(situation: str, client):
|
| 35 |
prompt = f"""
|
| 36 |
You are a wise assistant. Given a situation, respond with exactly:
|
|
|
|
| 46 |
Situation: {situation}
|
| 47 |
Answer:
|
| 48 |
"""
|
|
|
|
| 49 |
response = client.chat.completions.create(
|
| 50 |
messages=[{"role": "user", "content": prompt}],
|
| 51 |
max_tokens=150
|
| 52 |
)
|
|
|
|
| 53 |
generated_text = response.choices[0].message.content.strip()
|
| 54 |
|
|
|
|
| 55 |
lines = [line.strip() for line in generated_text.split("\n") if line.strip()]
|
| 56 |
if len(lines) >= 3:
|
| 57 |
idiom = lines[0]
|
| 58 |
pinyin = lines[1]
|
| 59 |
meaning = " ".join(lines[2:])
|
| 60 |
+
explanation = f"{pinyin}<br><br>{meaning}"
|
| 61 |
else:
|
| 62 |
idiom = generated_text
|
| 63 |
explanation = ""
|
|
|
|
| 64 |
return idiom, explanation
|
| 65 |
|
| 66 |
+
# ======================
|
| 67 |
+
# UI logic
|
| 68 |
+
# ======================
|
| 69 |
+
def update_ui(situation):
|
| 70 |
+
if USE_MOCK:
|
| 71 |
+
idiom, explanation = generate_idiom_mock(situation)
|
| 72 |
+
else:
|
| 73 |
+
idiom, explanation = generate_idiom(situation, client)
|
| 74 |
+
return f"<div class='idiom-output'>{idiom}</div>", f"<div class='explanation-output'>{explanation}</div>"
|
| 75 |
|
| 76 |
def launch_app():
|
| 77 |
+
with gr.Blocks(css="style.css") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
gr.Markdown("## 🀄 Chinese Wisdom Generator\nEnter a situation, get a Chinese idiom with explanation.")
|
|
|
|
| 79 |
with gr.Row():
|
|
|
|
| 80 |
with gr.Column(scale=1):
|
| 81 |
+
situation = gr.Textbox(
|
| 82 |
label="Describe your situation...",
|
| 83 |
placeholder="e.g. I procrastinated on my homework again...",
|
| 84 |
lines=3
|
| 85 |
)
|
| 86 |
submit_btn = gr.Button("✨ Find Idiom")
|
|
|
|
| 87 |
gr.Examples(
|
| 88 |
examples=[
|
| 89 |
["I studied hard but still failed my exam."],
|
| 90 |
["I missed my bus because I woke up late."],
|
| 91 |
["I finally finished a long project after months."],
|
| 92 |
],
|
| 93 |
+
inputs=[situation]
|
| 94 |
)
|
|
|
|
|
|
|
| 95 |
with gr.Column(scale=1):
|
| 96 |
idiom_output = gr.HTML("<div class='idiom-output'>—</div>")
|
| 97 |
explanation_output = gr.HTML("<div class='explanation-output'>—</div>")
|
| 98 |
|
| 99 |
+
submit_btn.click(update_ui, inputs=[situation], outputs=[idiom_output, explanation_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
demo.launch(debug=True)
|
| 101 |
|
|
|
|
| 102 |
if __name__ == "__main__":
|
| 103 |
launch_app()
|