Mel Seto
commited on
Commit
·
977dd1c
1
Parent(s):
108259c
adjust UI so it hopefully works on Spaces prod
Browse files
app.py
CHANGED
|
@@ -3,25 +3,27 @@ import os
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
-
load_dotenv()
|
| 7 |
-
|
| 8 |
# ======================
|
| 9 |
-
#
|
| 10 |
# ======================
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# ======================
|
| 14 |
-
# Instantiate client (
|
| 15 |
# ======================
|
|
|
|
| 16 |
if not USE_MOCK:
|
| 17 |
client = InferenceClient(
|
| 18 |
provider="cerebras",
|
| 19 |
-
model=
|
| 20 |
api_key=os.environ["HF_TOKEN"]
|
| 21 |
)
|
| 22 |
|
| 23 |
# ======================
|
| 24 |
-
# Mock function for testing
|
| 25 |
# ======================
|
| 26 |
def generate_idiom_mock(situation: str):
|
| 27 |
idiom = "对症下药"
|
|
@@ -47,12 +49,14 @@ 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]
|
|
@@ -61,43 +65,62 @@ Answer:
|
|
| 61 |
else:
|
| 62 |
idiom = generated_text
|
| 63 |
explanation = ""
|
|
|
|
| 64 |
return idiom, explanation
|
| 65 |
|
| 66 |
# ======================
|
| 67 |
-
# UI
|
| 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("
|
|
|
|
| 79 |
with gr.Row():
|
| 80 |
-
with gr.Column(
|
| 81 |
situation = gr.Textbox(
|
| 82 |
-
label="
|
| 83 |
-
|
| 84 |
-
|
| 85 |
)
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
gr.Examples(
|
| 88 |
examples=[
|
| 89 |
-
["
|
| 90 |
-
["
|
| 91 |
-
["
|
|
|
|
|
|
|
| 92 |
],
|
| 93 |
-
inputs=
|
| 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 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
launch_app()
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
|
|
|
|
|
|
|
| 6 |
# ======================
|
| 7 |
+
# Config
|
| 8 |
# ======================
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
MODEL = "meta-llama/Llama-3.3-70B-Instruct"
|
| 12 |
+
USE_MOCK = False # ✅ Toggle between mock and real API
|
| 13 |
|
| 14 |
# ======================
|
| 15 |
+
# Instantiate client (if not mocking)
|
| 16 |
# ======================
|
| 17 |
+
client = None
|
| 18 |
if not USE_MOCK:
|
| 19 |
client = InferenceClient(
|
| 20 |
provider="cerebras",
|
| 21 |
+
model=MODEL,
|
| 22 |
api_key=os.environ["HF_TOKEN"]
|
| 23 |
)
|
| 24 |
|
| 25 |
# ======================
|
| 26 |
+
# Mock function for UI testing
|
| 27 |
# ======================
|
| 28 |
def generate_idiom_mock(situation: str):
|
| 29 |
idiom = "对症下药"
|
|
|
|
| 49 |
Answer:
|
| 50 |
"""
|
| 51 |
response = client.chat.completions.create(
|
| 52 |
+
model=MODEL,
|
| 53 |
messages=[{"role": "user", "content": prompt}],
|
| 54 |
max_tokens=150
|
| 55 |
)
|
|
|
|
| 56 |
|
| 57 |
+
generated_text = response.choices[0].message.content.strip()
|
| 58 |
lines = [line.strip() for line in generated_text.split("\n") if line.strip()]
|
| 59 |
+
|
| 60 |
if len(lines) >= 3:
|
| 61 |
idiom = lines[0]
|
| 62 |
pinyin = lines[1]
|
|
|
|
| 65 |
else:
|
| 66 |
idiom = generated_text
|
| 67 |
explanation = ""
|
| 68 |
+
|
| 69 |
return idiom, explanation
|
| 70 |
|
| 71 |
# ======================
|
| 72 |
+
# UI Wrapper
|
| 73 |
# ======================
|
| 74 |
def update_ui(situation):
|
| 75 |
if USE_MOCK:
|
| 76 |
idiom, explanation = generate_idiom_mock(situation)
|
| 77 |
else:
|
| 78 |
idiom, explanation = generate_idiom(situation, client)
|
|
|
|
| 79 |
|
| 80 |
+
return (
|
| 81 |
+
f"<div class='idiom-output'>{idiom}</div>",
|
| 82 |
+
f"<div class='explanation-output'>{explanation}</div>"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# ======================
|
| 86 |
+
# Launch app
|
| 87 |
+
# ======================
|
| 88 |
def launch_app():
|
| 89 |
with gr.Blocks(css="style.css") as demo:
|
| 90 |
+
gr.Markdown("# 🎋 Chinese Idioms Generator")
|
| 91 |
+
|
| 92 |
with gr.Row():
|
| 93 |
+
with gr.Column():
|
| 94 |
situation = gr.Textbox(
|
| 95 |
+
label="Enter a situation",
|
| 96 |
+
lines=2,
|
| 97 |
+
placeholder="e.g., When facing a big challenge"
|
| 98 |
)
|
| 99 |
+
generate_btn = gr.Button("✨ Generate Idiom")
|
| 100 |
+
|
| 101 |
+
# ✅ Example situations
|
| 102 |
gr.Examples(
|
| 103 |
examples=[
|
| 104 |
+
["When facing a big challenge"],
|
| 105 |
+
["When someone helps you in a time of need"],
|
| 106 |
+
["When you need to stay calm under pressure"],
|
| 107 |
+
["When teamwork is important to succeed"],
|
| 108 |
+
["When rushing leads to mistakes"]
|
| 109 |
],
|
| 110 |
+
inputs=situation
|
| 111 |
)
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
with gr.Column():
|
| 114 |
+
idiom_output = gr.HTML(label="Idiom")
|
| 115 |
+
explanation_output = gr.HTML(label="Explanation")
|
| 116 |
+
|
| 117 |
+
generate_btn.click(
|
| 118 |
+
fn=update_ui,
|
| 119 |
+
inputs=situation,
|
| 120 |
+
outputs=[idiom_output, explanation_output]
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
demo.launch()
|
| 124 |
|
| 125 |
if __name__ == "__main__":
|
| 126 |
launch_app()
|