Commit
·
a9cdef6
1
Parent(s):
c50b20c
analogy update
Browse files
app.py
CHANGED
|
@@ -1,7 +1,63 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
+
# ---------------------------
|
| 6 |
+
# Hugging Face API Settings
|
| 7 |
+
# ---------------------------
|
| 8 |
+
HF_API_URL = "https://router.huggingface.co/v1/chat/completions"
|
| 9 |
+
HF_API_TOKEN = "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" # <-- Insert your token here
|
| 10 |
|
| 11 |
+
headers = {
|
| 12 |
+
"Authorization": f"Bearer {HF_API_TOKEN}",
|
| 13 |
+
"Content-Type": "application/json"
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# ---------------------------
|
| 17 |
+
# Function to generate analogy
|
| 18 |
+
# ---------------------------
|
| 19 |
+
def generate_analogy(topic, theme):
|
| 20 |
+
prompt = (
|
| 21 |
+
f"Explain the topic '{topic}' using an analogy themed around '{theme}'. "
|
| 22 |
+
f"Format the response using Markdown. Include headings, bold, italics, bullet points, "
|
| 23 |
+
f"and horizontal rules where appropriate."
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
payload = {
|
| 27 |
+
"model": "deepseek-ai/DeepSeek-V3.2:novita",
|
| 28 |
+
"stream": False,
|
| 29 |
+
"messages": [{"role": "user", "content": prompt}]
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
response = requests.post(HF_API_URL, headers=headers, data=json.dumps(payload))
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
data = response.json()
|
| 36 |
+
except Exception:
|
| 37 |
+
return "Error: Could not decode API response."
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
return data["choices"][0]["message"]["content"]
|
| 41 |
+
except Exception:
|
| 42 |
+
return f"Unexpected response format:\n\n{data}"
|
| 43 |
+
|
| 44 |
+
# ---------------------------
|
| 45 |
+
# Gradio UI
|
| 46 |
+
# ---------------------------
|
| 47 |
+
themes = ["Love Island", "One Piece", "Spiderman", "Premier League"]
|
| 48 |
+
|
| 49 |
+
demo = gr.Interface(
|
| 50 |
+
fn=generate_analogy,
|
| 51 |
+
inputs=[
|
| 52 |
+
gr.Textbox(label="Enter any topic", placeholder="e.g. Quantum Physics"),
|
| 53 |
+
gr.Dropdown(choices=themes, label="Choose a Theme")
|
| 54 |
+
],
|
| 55 |
+
outputs=gr.Markdown(label="Generated Analogy"), # <-- Rendered Markdown
|
| 56 |
+
title="Simple Analogy App",
|
| 57 |
+
description="Turn any topic into a themed analogy using DeepSeek AI with rendered Markdown!"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# ---------------------------
|
| 61 |
+
# Run the app
|
| 62 |
+
# ---------------------------
|
| 63 |
demo.launch()
|