saherPervaiz commited on
Commit
278623e
·
verified ·
1 Parent(s): fe80ae7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -107
app.py CHANGED
@@ -9,7 +9,6 @@ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-capt
9
  # Function to generate description for an image using BLIP
10
  def describe_image(image: Image.Image):
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)
@@ -17,154 +16,71 @@ def describe_image(image: Image.Image):
17
  except Exception as e:
18
  return f"Error describing the image: {e}"
19
 
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
-
37
- # Format chat history for display
38
- formatted_history = "\n".join(
39
- [f"{role}: {message}" for role, message in chat_history]
40
- )
41
  return formatted_history, chat_history
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;
49
  color: #333;
50
  font-family: 'Roboto', sans-serif;
51
- display: flex;
52
- justify-content: center;
53
- align-items: center;
54
- height: 100vh;
55
- margin: 0;
56
  }
57
  .gradio-container {
58
- width: 70%;
59
- max-width: 500px;
 
60
  background-color: #ffffff;
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;
73
- }
74
- .gradio-container .textbox:focus, .gradio-container .button:focus {
75
- border-color: #007bff;
76
- box-shadow: 0px 0px 5px rgba(0, 123, 255, 0.5);
77
- }
78
- .gradio-container .button:hover {
79
- background-color: #007bff;
80
- color: #fff;
81
- cursor: pointer;
82
- }
83
- .textbox {
84
- margin-bottom: 20px;
85
  }
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;
93
- color: #333;
94
  margin-bottom: 20px;
95
  font-size: 14px;
96
  line-height: 1.6;
97
  }
98
- .user-message {
99
- background-color: #e0f7fa;
100
- padding: 10px;
101
- border-radius: 10px;
102
- margin-bottom: 8px;
103
- max-width: 75%;
104
- }
105
- .ai-message {
106
- background-color: #e8f5e9;
107
- padding: 10px;
108
- border-radius: 10px;
109
- margin-bottom: 8px;
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",
129
- placeholder="Ask me anything!",
130
- lines=2,
131
- interactive=True,
132
- )
133
- submit_button = gr.Button("Send")
134
  clear_button = gr.Button("Clear Chat")
 
135
 
136
- chatbot_output = gr.Textbox(
137
- label="Chat History",
138
- placeholder="AI's responses will appear here.",
139
- lines=15,
140
- interactive=False,
141
- elem_id="chatbox",
142
- )
143
 
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
- )
151
-
152
- # State to hold chat history
153
  chat_history = gr.State([])
154
 
155
- # Button functionalities
156
- submit_button.click(
157
- fn=chat,
158
- inputs=[user_input, chat_history, image_input],
159
- outputs=[chatbot_output, chat_history],
160
- )
161
-
162
- clear_button.click(
163
- fn=lambda: ("", []),
164
- inputs=[],
165
- outputs=[chatbot_output, chat_history],
166
- )
167
 
168
- # Launch locally
169
  if __name__ == "__main__":
170
  demo.launch()
 
9
  # Function to generate description for an image using BLIP
10
  def describe_image(image: Image.Image):
11
  try:
 
12
  inputs = processor(images=image, return_tensors="pt")
13
  out = model.generate(**inputs)
14
  description = processor.decode(out[0], skip_special_tokens=True)
 
16
  except Exception as e:
17
  return f"Error describing the image: {e}"
18
 
19
+ # Chatbot logic
20
  def chat(user_input, chat_history, image):
21
  try:
22
+ response = f"AI Response: {user_input}"
 
 
 
23
  if image is not None:
24
  image_description = describe_image(image)
25
  response += f"\n\n[Image Description]: {image_description}"
 
 
 
 
26
  chat_history.append(("User", user_input))
27
  chat_history.append(("AI", response))
28
+ formatted_history = "\n".join([f"{role}: {msg}" for role, msg in chat_history])
 
 
 
 
29
  return formatted_history, chat_history
30
  except Exception as e:
31
  return f"Error: {e}", chat_history
32
 
33
+ # Build Gradio interface
34
  with gr.Blocks(css="""
35
  body {
36
  background-color: #f7f7f7;
37
  color: #333;
38
  font-family: 'Roboto', sans-serif;
 
 
 
 
 
39
  }
40
  .gradio-container {
41
+ width: 100%;
42
+ max-width: 700px;
43
+ margin: auto;
44
  background-color: #ffffff;
45
  padding: 30px;
46
  border-radius: 15px;
47
  box-shadow: 0px 10px 30px rgba(0, 0, 0, 0.1);
 
48
  }
49
+ .textbox, .button {
50
+ margin-bottom: 15px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  }
52
  #chatbox {
53
+ height: 300px;
54
  overflow-y: auto;
55
  border: 1px solid #dcdcdc;
56
  padding: 20px;
57
  border-radius: 10px;
58
  background-color: #f9f9f9;
 
59
  margin-bottom: 20px;
60
  font-size: 14px;
61
  line-height: 1.6;
62
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  """) as demo:
64
+
65
+ gr.Markdown("## 🤖 **AI Chatbot with Image Captioning (BLIP + Gradio)**")
66
+ gr.Markdown("Upload an image to generate a caption, then chat with the bot.")
67
 
 
68
  with gr.Column():
69
+ user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", lines=2)
70
+ submit_button = gr.Button("Send Message")
 
 
 
 
 
71
  clear_button = gr.Button("Clear Chat")
72
+ chatbot_output = gr.Textbox(label="Chat History", lines=12, interactive=False, elem_id="chatbox")
73
 
74
+ image_input = gr.Image(label="Upload Image", type="pil", elem_id="image-upload")
75
+ upload_button = gr.Button("Describe Image")
76
+ image_caption = gr.Textbox(label="Image Description", interactive=False)
 
 
 
 
77
 
 
 
 
 
 
 
 
 
 
78
  chat_history = gr.State([])
79
 
80
+ submit_button.click(fn=chat, inputs=[user_input, chat_history, image_input], outputs=[chatbot_output, chat_history])
81
+ clear_button.click(fn=lambda: ("", []), inputs=[], outputs=[chatbot_output, chat_history])
82
+ upload_button.click(fn=describe_image, inputs=[image_input], outputs=[image_caption])
 
 
 
 
 
 
 
 
 
83
 
84
+ # Launch the app
85
  if __name__ == "__main__":
86
  demo.launch()