Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
3 |
-
import torch
|
4 |
from PIL import Image
|
5 |
|
6 |
# Load the BLIP image captioning model and processor
|
@@ -12,11 +11,8 @@ def describe_image(image: Image.Image):
|
|
12 |
try:
|
13 |
# Preprocess the image and pass it to the model
|
14 |
inputs = processor(images=image, return_tensors="pt")
|
15 |
-
|
16 |
-
# Generate caption using the model
|
17 |
out = model.generate(**inputs)
|
18 |
description = processor.decode(out[0], skip_special_tokens=True)
|
19 |
-
|
20 |
return description
|
21 |
except Exception as e:
|
22 |
return f"Error describing the image: {e}"
|
@@ -24,17 +20,17 @@ def describe_image(image: Image.Image):
|
|
24 |
# Define chatbot interaction function
|
25 |
def chat(user_input, chat_history, image):
|
26 |
try:
|
27 |
-
# Generate text response (
|
28 |
-
response = f"AI Response: {user_input}"
|
29 |
|
30 |
-
# If an image is uploaded, describe it
|
31 |
if image is not None:
|
32 |
image_description = describe_image(image)
|
33 |
response += f"\n\n[Image Description]: {image_description}"
|
34 |
else:
|
35 |
image_description = "No image uploaded."
|
36 |
|
37 |
-
# Update chat history with
|
38 |
chat_history.append(("User", user_input))
|
39 |
chat_history.append(("AI", response))
|
40 |
|
@@ -46,7 +42,7 @@ def chat(user_input, chat_history, image):
|
|
46 |
except Exception as e:
|
47 |
return f"Error: {e}", chat_history
|
48 |
|
49 |
-
# Create Gradio interface with updated
|
50 |
with gr.Blocks(css="""
|
51 |
body {
|
52 |
background-color: #f7f7f7;
|
@@ -65,12 +61,12 @@ with gr.Blocks(css="""
|
|
65 |
padding: 30px;
|
66 |
border-radius: 15px;
|
67 |
box-shadow: 0px 10px 30px rgba(0, 0, 0, 0.1);
|
68 |
-
border: 1px solid #dcdcdc;
|
69 |
}
|
70 |
.gradio-container .textbox, .gradio-container .button {
|
71 |
background-color: #eeeeee;
|
72 |
color: #333;
|
73 |
-
border: 1px solid #dcdcdc;
|
74 |
border-radius: 10px;
|
75 |
padding: 12px;
|
76 |
font-size: 16px;
|
@@ -90,7 +86,7 @@ with gr.Blocks(css="""
|
|
90 |
#chatbox {
|
91 |
height: 350px;
|
92 |
overflow-y: auto;
|
93 |
-
border: 1px solid #dcdcdc;
|
94 |
padding: 20px;
|
95 |
border-radius: 10px;
|
96 |
background-color: #f9f9f9;
|
@@ -98,7 +94,6 @@ with gr.Blocks(css="""
|
|
98 |
margin-bottom: 20px;
|
99 |
font-size: 14px;
|
100 |
line-height: 1.6;
|
101 |
-
font-family: 'Arial', sans-serif;
|
102 |
}
|
103 |
.user-message {
|
104 |
background-color: #e0f7fa;
|
@@ -115,11 +110,19 @@ with gr.Blocks(css="""
|
|
115 |
max-width: 75%;
|
116 |
margin-left: auto;
|
117 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
""") as demo:
|
119 |
gr.Markdown("## 🤖 **Professional Groq Chatbot with Image Description**")
|
120 |
gr.Markdown("Type your message below or upload an image to get a description!")
|
121 |
|
122 |
-
#
|
123 |
with gr.Column():
|
124 |
user_input = gr.Textbox(
|
125 |
label="Your Message",
|
@@ -141,7 +144,7 @@ with gr.Blocks(css="""
|
|
141 |
# Image upload component
|
142 |
image_input = gr.Image(
|
143 |
label="Upload Image for Description",
|
144 |
-
type="pil",
|
145 |
interactive=True,
|
146 |
elem_id="image-upload",
|
147 |
)
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
|
|
3 |
from PIL import Image
|
4 |
|
5 |
# Load the BLIP image captioning model and processor
|
|
|
11 |
try:
|
12 |
# Preprocess the image and pass it to the model
|
13 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
|
14 |
out = model.generate(**inputs)
|
15 |
description = processor.decode(out[0], skip_special_tokens=True)
|
|
|
16 |
return description
|
17 |
except Exception as e:
|
18 |
return f"Error describing the image: {e}"
|
|
|
20 |
# Define chatbot interaction function
|
21 |
def chat(user_input, chat_history, image):
|
22 |
try:
|
23 |
+
# Generate text response (placeholder for now)
|
24 |
+
response = f"AI Response: {user_input}"
|
25 |
|
26 |
+
# If an image is uploaded, describe it
|
27 |
if image is not None:
|
28 |
image_description = describe_image(image)
|
29 |
response += f"\n\n[Image Description]: {image_description}"
|
30 |
else:
|
31 |
image_description = "No image uploaded."
|
32 |
|
33 |
+
# Update chat history with user input and AI response
|
34 |
chat_history.append(("User", user_input))
|
35 |
chat_history.append(("AI", response))
|
36 |
|
|
|
42 |
except Exception as e:
|
43 |
return f"Error: {e}", chat_history
|
44 |
|
45 |
+
# Create Gradio interface with updated styles
|
46 |
with gr.Blocks(css="""
|
47 |
body {
|
48 |
background-color: #f7f7f7;
|
|
|
61 |
padding: 30px;
|
62 |
border-radius: 15px;
|
63 |
box-shadow: 0px 10px 30px rgba(0, 0, 0, 0.1);
|
64 |
+
border: 1px solid #dcdcdc;
|
65 |
}
|
66 |
.gradio-container .textbox, .gradio-container .button {
|
67 |
background-color: #eeeeee;
|
68 |
color: #333;
|
69 |
+
border: 1px solid #dcdcdc;
|
70 |
border-radius: 10px;
|
71 |
padding: 12px;
|
72 |
font-size: 16px;
|
|
|
86 |
#chatbox {
|
87 |
height: 350px;
|
88 |
overflow-y: auto;
|
89 |
+
border: 1px solid #dcdcdc;
|
90 |
padding: 20px;
|
91 |
border-radius: 10px;
|
92 |
background-color: #f9f9f9;
|
|
|
94 |
margin-bottom: 20px;
|
95 |
font-size: 14px;
|
96 |
line-height: 1.6;
|
|
|
97 |
}
|
98 |
.user-message {
|
99 |
background-color: #e0f7fa;
|
|
|
110 |
max-width: 75%;
|
111 |
margin-left: auto;
|
112 |
}
|
113 |
+
#image-upload {
|
114 |
+
margin-top: 10px;
|
115 |
+
border: 1px solid #dcdcdc;
|
116 |
+
border-radius: 10px;
|
117 |
+
padding: 12px;
|
118 |
+
background-color: #ffffff;
|
119 |
+
width: 100%;
|
120 |
+
}
|
121 |
""") as demo:
|
122 |
gr.Markdown("## 🤖 **Professional Groq Chatbot with Image Description**")
|
123 |
gr.Markdown("Type your message below or upload an image to get a description!")
|
124 |
|
125 |
+
# Layout with vertical alignment
|
126 |
with gr.Column():
|
127 |
user_input = gr.Textbox(
|
128 |
label="Your Message",
|
|
|
144 |
# Image upload component
|
145 |
image_input = gr.Image(
|
146 |
label="Upload Image for Description",
|
147 |
+
type="pil",
|
148 |
interactive=True,
|
149 |
elem_id="image-upload",
|
150 |
)
|