TejAndrewsACC commited on
Commit
77bfbf0
·
verified ·
1 Parent(s): 75151fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -20
app.py CHANGED
@@ -1,32 +1,20 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- import os
4
 
5
  hf_key = os.getenv("HF_KEY").strip()
6
  # Initialize the Hugging Face client
7
  client = InferenceClient(api_key=hf_key)
8
 
9
  # Define the chatbot function
10
- def chat_with_model(text, image=None):
 
11
  messages = [{"role": "user", "content": {"type": "text", "text": text}}]
12
-
13
- # If an image is uploaded, encode it and send it as part of the payload
14
- if image:
15
- import base64
16
- from PIL import Image
17
- from io import BytesIO
18
-
19
- # Convert image to base64
20
- buffered = BytesIO()
21
- image.save(buffered, format="PNG")
22
- img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
23
-
24
- # Add the image content to the messages
25
  messages.append({
26
  "role": "user",
27
  "content": {
28
- "type": "image_base64",
29
- "image_base64": img_str
30
  }
31
  })
32
 
@@ -44,11 +32,10 @@ ui = gr.Interface(
44
  fn=chat_with_model,
45
  inputs=[
46
  gr.Textbox(label="Enter your message"),
47
- gr.Image(type="pil", label="Upload an Image (Optional)")
48
  ],
49
  outputs=gr.Textbox(label="Response from the chatbot"),
50
- title="AI Chatbot with Direct Image Upload",
51
- theme="TejAndrewsACC/zetaofficalthemeacc"
52
  )
53
 
54
  # Launch the UI
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
 
4
  hf_key = os.getenv("HF_KEY").strip()
5
  # Initialize the Hugging Face client
6
  client = InferenceClient(api_key=hf_key)
7
 
8
  # Define the chatbot function
9
+ def chat_with_model(text, image_url=None):
10
+ # Prepare messages
11
  messages = [{"role": "user", "content": {"type": "text", "text": text}}]
12
+ if image_url:
 
 
 
 
 
 
 
 
 
 
 
 
13
  messages.append({
14
  "role": "user",
15
  "content": {
16
+ "type": "image_url",
17
+ "image_url": {"url": image_url}
18
  }
19
  })
20
 
 
32
  fn=chat_with_model,
33
  inputs=[
34
  gr.Textbox(label="Enter your message"),
35
+ gr.Textbox(label="Optional Image URL (Leave empty if not needed)")
36
  ],
37
  outputs=gr.Textbox(label="Response from the chatbot"),
38
+ title="AI Chatbot with Image Processing"
 
39
  )
40
 
41
  # Launch the UI