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

Update app.py to use HF secrets, add Markdown output

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -1,12 +1,17 @@
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}",
@@ -29,35 +34,35 @@ def generate_analogy(topic, theme):
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()
 
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.")
15
 
16
  headers = {
17
  "Authorization": f"Bearer {HF_API_TOKEN}",
 
34
  "messages": [{"role": "user", "content": prompt}]
35
  }
36
 
 
 
37
  try:
38
+ response = requests.post(HF_API_URL, headers=headers, data=json.dumps(payload))
39
  data = response.json()
 
 
 
 
40
  return data["choices"][0]["message"]["content"]
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()