cstr commited on
Commit
25aa6b5
·
verified ·
1 Parent(s): 1ca78b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -2,8 +2,9 @@ import gradio as gr
2
  import requests
3
  import json
4
  import os
 
5
 
6
- # Set OpenRouter API key in the Space's secrets as "OPENROUTER_API_KEY"
7
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
8
  HEADERS = {
9
  "Authorization": f"Bearer {OPENROUTER_API_KEY}",
@@ -19,19 +20,16 @@ FREE_MODELS = {
19
  "Qwen: Qwen2.5 VL 72B Instruct (free)": ("qwen/qwen2.5-vl-72b-instruct:free", 131072),
20
  }
21
 
22
-
23
  def query_openrouter_model(model_id, prompt, image=None):
24
- messages = [{"role": "user", "content": prompt}]
25
-
26
- # If image is included, add it to the message content as a dict
27
  if image is not None:
28
  with open(image, "rb") as f:
29
- image_bytes = f.read()
30
- base64_image = base64.b64encode(image_bytes).decode("utf-8")
31
- messages[0]["content"] = [
32
- {"type": "text", "text": prompt},
33
- {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}
34
- ]
35
 
36
  payload = {
37
  "model": model_id,
@@ -51,15 +49,13 @@ def query_openrouter_model(model_id, prompt, image=None):
51
  except Exception as e:
52
  return f"Error: {str(e)}\n{response.text}"
53
 
54
-
55
  def chat_interface(prompt, image, model_label):
56
  model_id, _ = FREE_MODELS[model_label]
57
  return query_openrouter_model(model_id, prompt, image)
58
 
59
-
60
  with gr.Blocks(title="CrispChat") as demo:
61
  gr.Markdown("""
62
- # 🌟 CrispChat
63
  Multi-modal chat with free OpenRouter models
64
  """)
65
 
@@ -79,4 +75,5 @@ with gr.Blocks(title="CrispChat") as demo:
79
  submit.click(fn=chat_interface, inputs=[prompt, image, model_choice], outputs=output)
80
 
81
  if __name__ == "__main__":
82
- demo.launch()
 
 
2
  import requests
3
  import json
4
  import os
5
+ import base64
6
 
7
+ # Set your OpenRouter API key in the Space's secrets as "OPENROUTER_API_KEY"
8
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
9
  HEADERS = {
10
  "Authorization": f"Bearer {OPENROUTER_API_KEY}",
 
20
  "Qwen: Qwen2.5 VL 72B Instruct (free)": ("qwen/qwen2.5-vl-72b-instruct:free", 131072),
21
  }
22
 
 
23
  def query_openrouter_model(model_id, prompt, image=None):
24
+ # If there's an image, embed it in the prompt as text
 
 
25
  if image is not None:
26
  with open(image, "rb") as f:
27
+ b64_img = base64.b64encode(f.read()).decode("utf-8")
28
+ prompt += f"\n[User attached an image: data:image/png;base64,{b64_img}]"
29
+
30
+ messages = [
31
+ {"role": "user", "content": prompt}
32
+ ]
33
 
34
  payload = {
35
  "model": model_id,
 
49
  except Exception as e:
50
  return f"Error: {str(e)}\n{response.text}"
51
 
 
52
  def chat_interface(prompt, image, model_label):
53
  model_id, _ = FREE_MODELS[model_label]
54
  return query_openrouter_model(model_id, prompt, image)
55
 
 
56
  with gr.Blocks(title="CrispChat") as demo:
57
  gr.Markdown("""
58
+ # 🌟 CrispChat
59
  Multi-modal chat with free OpenRouter models
60
  """)
61
 
 
75
  submit.click(fn=chat_interface, inputs=[prompt, image, model_choice], outputs=output)
76
 
77
  if __name__ == "__main__":
78
+ # Hide the API documentation to avoid schema issues
79
+ demo.launch(show_api=False)