Spaces:
Sleeping
Sleeping
Pavithiran
commited on
Commit
•
2b76ae3
1
Parent(s):
9a77a5b
Update app.py
Browse files
app.py
CHANGED
@@ -63,14 +63,6 @@
|
|
63 |
# if __name__ == "__main__":
|
64 |
# demo.launch()
|
65 |
|
66 |
-
import gradio as gr
|
67 |
-
from huggingface_hub import InferenceClient
|
68 |
-
from PIL import Image
|
69 |
-
import io
|
70 |
-
import base64
|
71 |
-
|
72 |
-
client = InferenceClient("meta-llama/Llama-3.2-11B-Vision-Instruct")
|
73 |
-
|
74 |
def respond(
|
75 |
message,
|
76 |
history: list[tuple[str, str]],
|
@@ -78,9 +70,8 @@ def respond(
|
|
78 |
max_tokens,
|
79 |
temperature,
|
80 |
top_p,
|
81 |
-
image: Image, #
|
82 |
):
|
83 |
-
# Prepare the system message and history for the conversation
|
84 |
messages = [{"role": "system", "content": system_message}]
|
85 |
|
86 |
for val in history:
|
@@ -89,48 +80,31 @@ def respond(
|
|
89 |
if val[1]:
|
90 |
messages.append({"role": "assistant", "content": val[1]})
|
91 |
|
92 |
-
# Add
|
93 |
messages.append({"role": "user", "content": message})
|
94 |
|
95 |
-
# Convert
|
96 |
image_bytes = io.BytesIO()
|
97 |
image.save(image_bytes, format='PNG')
|
98 |
image_bytes.seek(0)
|
99 |
image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
|
100 |
|
101 |
-
# Use InferenceClient to handle the image and text input to the model
|
102 |
-
# Pass the base64-encoded image as the input
|
103 |
-
response_data = client.text_to_image(images=image_base64, prompt=message) # Pass the base64 string as 'images'
|
104 |
-
|
105 |
-
# Debug: print the response data to check its content
|
106 |
-
print("Response Data: ", response_data)
|
107 |
-
|
108 |
try:
|
109 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
if isinstance(response_data, dict) and 'image' in response_data:
|
|
|
111 |
image_response = response_data['image']
|
112 |
-
# Decode the base64 image back into an image object
|
113 |
image_bytes = base64.b64decode(image_response)
|
114 |
image = Image.open(io.BytesIO(image_bytes))
|
115 |
-
image.show() #
|
116 |
return "Image processed successfully"
|
117 |
else:
|
118 |
-
return f"Error:
|
119 |
except Exception as e:
|
120 |
return f"Error processing image: {e}"
|
121 |
-
|
122 |
-
# Create the Gradio interface with an image input
|
123 |
-
demo = gr.ChatInterface(
|
124 |
-
respond,
|
125 |
-
additional_inputs=[
|
126 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
127 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
128 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
129 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
130 |
-
gr.Image(type="pil", label="Upload an Image"), # Image input for vision tasks
|
131 |
-
],
|
132 |
-
)
|
133 |
-
|
134 |
-
if __name__ == "__main__":
|
135 |
-
demo.launch(share=True)
|
136 |
-
# Set share=True to create a public link
|
|
|
63 |
# if __name__ == "__main__":
|
64 |
# demo.launch()
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
def respond(
|
67 |
message,
|
68 |
history: list[tuple[str, str]],
|
|
|
70 |
max_tokens,
|
71 |
temperature,
|
72 |
top_p,
|
73 |
+
image: Image, # Image input
|
74 |
):
|
|
|
75 |
messages = [{"role": "system", "content": system_message}]
|
76 |
|
77 |
for val in history:
|
|
|
80 |
if val[1]:
|
81 |
messages.append({"role": "assistant", "content": val[1]})
|
82 |
|
83 |
+
# Add current user message
|
84 |
messages.append({"role": "user", "content": message})
|
85 |
|
86 |
+
# Convert image to base64
|
87 |
image_bytes = io.BytesIO()
|
88 |
image.save(image_bytes, format='PNG')
|
89 |
image_bytes.seek(0)
|
90 |
image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
try:
|
93 |
+
# Pass the base64 image and prompt
|
94 |
+
response_data = client.text_to_image(images=image_base64, prompt=message)
|
95 |
+
|
96 |
+
# Print out the response data for debugging
|
97 |
+
print("Response Data:", response_data)
|
98 |
+
|
99 |
+
# Check if the response contains an image
|
100 |
if isinstance(response_data, dict) and 'image' in response_data:
|
101 |
+
# Decode the base64 image response
|
102 |
image_response = response_data['image']
|
|
|
103 |
image_bytes = base64.b64decode(image_response)
|
104 |
image = Image.open(io.BytesIO(image_bytes))
|
105 |
+
image.show() # Display the image or return it
|
106 |
return "Image processed successfully"
|
107 |
else:
|
108 |
+
return f"Error: No valid image found in response. Response: {response_data}"
|
109 |
except Exception as e:
|
110 |
return f"Error processing image: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|