dominiconorton commited on
Commit
51306fe
·
1 Parent(s): 9fe12e0

Add loading state

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -3,7 +3,9 @@ import requests
3
  import json
4
  import os
5
 
 
6
  # Hugging Face API Settings
 
7
  HF_API_URL = "https://router.huggingface.co/v1/chat/completions"
8
  HF_API_TOKEN = os.environ.get("HF_TOKEN")
9
  if HF_API_TOKEN is None:
@@ -14,12 +16,17 @@ headers = {
14
  "Content-Type": "application/json"
15
  }
16
 
17
- # Function to generate analogy
 
 
18
  def generate_analogy(topic, theme):
 
 
 
19
  prompt = (
20
  f"Explain the topic '{topic}' using an analogy themed around '{theme}'. "
21
- f"Format the response using Markdown. Include headings, bold, italics, bullet points, "
22
- f"and horizontal rules where appropriate."
23
  )
24
 
25
  payload = {
@@ -31,31 +38,38 @@ def generate_analogy(topic, theme):
31
  try:
32
  response = requests.post(HF_API_URL, headers=headers, data=json.dumps(payload))
33
  data = response.json()
34
- return data["choices"][0]["message"]["content"]
 
 
35
  except Exception:
36
- return "Error: Could not generate analogy."
37
 
38
- # Theme options
 
 
39
  themes = ["Love Island", "One Piece", "Spiderman", "Premier League"]
40
 
41
- # Gradio Blocks UI
42
  with gr.Blocks() as demo:
43
  gr.Markdown("## Simple Analogy App")
44
- gr.Markdown("Turn any topic into a themed analogy using DeepSeek AI!")
45
 
46
  with gr.Row():
47
  topic_input = gr.Textbox(label="Enter any topic", placeholder="e.g. Quantum Physics")
48
  theme_input = gr.Dropdown(choices=themes, label="Choose a Theme")
49
 
50
  generate_button = gr.Button("Generate Analogy")
51
- output = gr.Markdown(label="Generated Analogy")
 
 
 
 
 
52
 
53
- # Connect button click to function with loading spinner
54
  generate_button.click(
55
  fn=generate_analogy,
56
  inputs=[topic_input, theme_input],
57
- outputs=output,
58
- show_progress=True # <-- This shows the spinner while generating
59
  )
60
 
61
  demo.launch()
 
3
  import json
4
  import os
5
 
6
+ # ---------------------------
7
  # Hugging Face API Settings
8
+ # ---------------------------
9
  HF_API_URL = "https://router.huggingface.co/v1/chat/completions"
10
  HF_API_TOKEN = os.environ.get("HF_TOKEN")
11
  if HF_API_TOKEN is None:
 
16
  "Content-Type": "application/json"
17
  }
18
 
19
+ # ---------------------------
20
+ # Function to generate analogy with loading placeholder
21
+ # ---------------------------
22
  def generate_analogy(topic, theme):
23
+ # Immediately show loading text in the Textbox
24
+ yield "⏳ Generating analogy...\nPlease wait."
25
+
26
  prompt = (
27
  f"Explain the topic '{topic}' using an analogy themed around '{theme}'. "
28
+ f"Use clear, beginner-friendly language. Include line breaks and short paragraphs "
29
+ f"so it can be read in a Textbox."
30
  )
31
 
32
  payload = {
 
38
  try:
39
  response = requests.post(HF_API_URL, headers=headers, data=json.dumps(payload))
40
  data = response.json()
41
+ # Preserve line breaks for Textbox
42
+ text = data["choices"][0]["message"]["content"].replace("\r\n", "\n")
43
+ yield text
44
  except Exception:
45
+ yield "Error: Could not generate analogy."
46
 
47
+ # ---------------------------
48
+ # Gradio UI
49
+ # ---------------------------
50
  themes = ["Love Island", "One Piece", "Spiderman", "Premier League"]
51
 
 
52
  with gr.Blocks() as demo:
53
  gr.Markdown("## Simple Analogy App")
54
+ gr.Markdown("Turn any topic into a themed analogy!")
55
 
56
  with gr.Row():
57
  topic_input = gr.Textbox(label="Enter any topic", placeholder="e.g. Quantum Physics")
58
  theme_input = gr.Dropdown(choices=themes, label="Choose a Theme")
59
 
60
  generate_button = gr.Button("Generate Analogy")
61
+ output = gr.Textbox(
62
+ label="Generated Analogy",
63
+ value="", # always visible
64
+ lines=20,
65
+ max_lines=40
66
+ )
67
 
68
+ # Connect button to generator function
69
  generate_button.click(
70
  fn=generate_analogy,
71
  inputs=[topic_input, theme_input],
72
+ outputs=output
 
73
  )
74
 
75
  demo.launch()