Petzys commited on
Commit
97cd1ed
·
1 Parent(s): 21f15e9

feat: image response

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +25 -4
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🦙
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.42.0
8
  app_file: app.py
9
  pinned: false
10
  hf_oauth: true
 
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 5.44.1
8
  app_file: app.py
9
  pinned: false
10
  hf_oauth: true
app.py CHANGED
@@ -63,8 +63,7 @@ def respond(
63
  oauth: gr.OAuthToken | None = None, # Gradio injects this when available
64
  ):
65
  if not oauth:
66
- yield "⚠️ Please sign in with your Hugging Face account (top of the page)"
67
- return
68
  token = oauth.token
69
 
70
  # Embed the query and search FAISS
@@ -81,7 +80,11 @@ Here are candidate xkcd comics:
81
  {context}
82
 
83
  Which comic fits best and why?
84
- Please answer with the comic ID, URL (https://xkcd.com/ID/) and a short explanation.
 
 
 
 
85
  """
86
 
87
  print("[PROMPT] " + prompt)
@@ -106,7 +109,25 @@ Please answer with the comic ID, URL (https://xkcd.com/ID/) and a short explanat
106
  except Exception:
107
  out = str(resp)
108
 
109
- yield out.strip() or "Sorry, I couldn't parse the model response."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  # --- UI ---
112
  with gr.Blocks(theme='gstaff/xkcd') as demo:
 
63
  oauth: gr.OAuthToken | None = None, # Gradio injects this when available
64
  ):
65
  if not oauth:
66
+ return "⚠️ Please sign in with your Hugging Face account (top of the page)"
 
67
  token = oauth.token
68
 
69
  # Embed the query and search FAISS
 
80
  {context}
81
 
82
  Which comic fits best and why?
83
+ Please answer with the comic ID, URL (https://xkcd.com/ID/) and a short explanation in the format:
84
+
85
+ [ID] URL
86
+
87
+ EXPLANATION
88
  """
89
 
90
  print("[PROMPT] " + prompt)
 
109
  except Exception:
110
  out = str(resp)
111
 
112
+ out_text = out.strip() or "Sorry, I couldn't parse the model response."
113
+
114
+ if out_text != "Sorry, I couldn't parse the model response.":
115
+ try:
116
+ id_start = out_text.index("[") +1
117
+ id_end = out_text.index("]")
118
+ id = out_text[id_start:id_end]
119
+ print(f'Read ID: {id}')
120
+
121
+ import urllib.request, json
122
+ with urllib.request.urlopen(f'https://xkcd.com/{id}/info.0.json') as url:
123
+ img_url = json.load(url)["img"]
124
+ print(f'Got image url: {img_url}')
125
+
126
+ return [out_text, gr.Image(value=img_url)]
127
+ except ValueError:
128
+ print("Couldn't parse xkcd ID or get image! That should not happen.")
129
+
130
+ return out_text
131
 
132
  # --- UI ---
133
  with gr.Blocks(theme='gstaff/xkcd') as demo: