Spaces:
Sleeping
Sleeping
Pavithiran
commited on
Commit
•
2c884a8
1
Parent(s):
2b76ae3
Update app.py
Browse files
app.py
CHANGED
@@ -63,48 +63,50 @@
|
|
63 |
# if __name__ == "__main__":
|
64 |
# demo.launch()
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
):
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
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 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
|
92 |
try:
|
93 |
-
#
|
94 |
response_data = client.text_to_image(images=image_base64, prompt=message)
|
|
|
|
|
|
|
95 |
|
96 |
-
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
# if __name__ == "__main__":
|
64 |
# demo.launch()
|
65 |
|
66 |
+
from PIL import Image
|
67 |
+
import base64
|
68 |
+
import io
|
69 |
+
import gradio as gr
|
70 |
+
from huggingface_hub import InferenceClient
|
71 |
+
import requests
|
72 |
+
|
73 |
+
# Function to convert image to base64
|
74 |
+
def image_to_base64(image: Image):
|
75 |
+
buffered = io.BytesIO()
|
76 |
+
image.save(buffered, format="PNG")
|
77 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
78 |
+
|
79 |
+
# Function to respond to input
|
80 |
+
def respond(message: str, image: Image):
|
|
|
|
|
|
|
|
|
|
|
81 |
# Convert image to base64
|
82 |
+
image_base64 = image_to_base64(image)
|
83 |
+
|
84 |
+
# Initialize the Hugging Face client
|
85 |
+
client = InferenceClient("your_huggingface_model")
|
86 |
|
87 |
try:
|
88 |
+
# Call the text-to-image method
|
89 |
response_data = client.text_to_image(images=image_base64, prompt=message)
|
90 |
+
|
91 |
+
# Convert the response data (image) into a PIL Image
|
92 |
+
image_response = Image.open(io.BytesIO(response_data))
|
93 |
|
94 |
+
return image_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
except Exception as e:
|
96 |
+
return str(e)
|
97 |
+
|
98 |
+
# Define the Gradio interface
|
99 |
+
def create_interface():
|
100 |
+
with gr.Blocks() as demo:
|
101 |
+
chatbot = gr.Chatbot(type='messages') # 'messages' format for chatbot
|
102 |
+
message_input = gr.Textbox()
|
103 |
+
image_input = gr.Image(type='pil') # Image input as PIL image
|
104 |
+
|
105 |
+
# Define the interaction
|
106 |
+
message_input.submit(respond, inputs=[message_input, image_input], outputs=[chatbot])
|
107 |
+
|
108 |
+
return demo
|
109 |
+
|
110 |
+
# Launch the interface
|
111 |
+
if __name__ == "__main__":
|
112 |
+
create_interface().launch(share=True) # Set share=True if you want to share the link publicly
|