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

Add generating spinner

Browse files
Files changed (1) hide show
  1. app.py +8 -15
app.py CHANGED
@@ -1,14 +1,10 @@
1
  import gradio as gr
2
  import requests
3
  import json
4
- import os # For reading the secret token
5
 
6
- # ---------------------------
7
  # Hugging Face API Settings
8
- # ---------------------------
9
  HF_API_URL = "https://router.huggingface.co/v1/chat/completions"
10
-
11
- # Read the token from environment variable HF_TOKEN (set in Space secrets)
12
  HF_API_TOKEN = os.environ.get("HF_TOKEN")
13
  if HF_API_TOKEN is None:
14
  raise ValueError("HF_TOKEN not found! Please add it as a secret in your Hugging Face Space.")
@@ -18,9 +14,7 @@ headers = {
18
  "Content-Type": "application/json"
19
  }
20
 
21
- # ---------------------------
22
  # Function to generate analogy
23
- # ---------------------------
24
  def generate_analogy(topic, theme):
25
  prompt = (
26
  f"Explain the topic '{topic}' using an analogy themed around '{theme}'. "
@@ -41,28 +35,27 @@ def generate_analogy(topic, theme):
41
  except Exception:
42
  return "Error: Could not generate analogy."
43
 
44
- # ---------------------------
45
- # Gradio UI
46
- # ---------------------------
47
  themes = ["Love Island", "One Piece", "Spiderman", "Premier League"]
48
 
 
49
  with gr.Blocks() as demo:
50
  gr.Markdown("## Simple Analogy App")
51
  gr.Markdown("Turn any topic into a themed analogy using DeepSeek AI!")
52
-
53
  with gr.Row():
54
  topic_input = gr.Textbox(label="Enter any topic", placeholder="e.g. Quantum Physics")
55
  theme_input = gr.Dropdown(choices=themes, label="Choose a Theme")
56
-
57
  generate_button = gr.Button("Generate Analogy")
58
- output = gr.Markdown(label="Generated Analogy") # Rendered Markdown
59
 
60
- # Connect button to function with visible loading
61
  generate_button.click(
62
  fn=generate_analogy,
63
  inputs=[topic_input, theme_input],
64
  outputs=output,
65
- show_progress=True # ensures spinner is visible
66
  )
67
 
68
  demo.launch()
 
1
  import gradio as gr
2
  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:
10
  raise ValueError("HF_TOKEN not found! Please add it as a secret in your Hugging Face Space.")
 
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}'. "
 
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()