saherPervaiz commited on
Commit
31a5b8f
·
verified ·
1 Parent(s): dff5c2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -36
app.py CHANGED
@@ -1,12 +1,17 @@
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
6
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
7
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
8
 
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")
@@ -16,62 +21,64 @@ def describe_image(image: Image.Image):
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
 
@@ -81,6 +88,6 @@ with gr.Blocks(css="""
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()
 
1
  import gradio as gr
2
  from transformers import BlipProcessor, BlipForConditionalGeneration
3
  from PIL import Image
4
+ from groq import Groq
5
+ from config import GROQ_API_KEY, MODEL_NAME
6
 
7
+ # === Load BLIP image captioning model ===
8
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
9
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
10
 
11
+ # === Initialize Groq client ===
12
+ client = Groq(api_key=GROQ_API_KEY)
13
+
14
+ # === Function to describe image using BLIP ===
15
  def describe_image(image: Image.Image):
16
  try:
17
  inputs = processor(images=image, return_tensors="pt")
 
21
  except Exception as e:
22
  return f"Error describing the image: {e}"
23
 
24
+ # === Generate response from Groq ===
25
+ def generate_response(user_input):
26
+ try:
27
+ response = client.chat.completions.create(
28
+ messages=[{"role": "user", "content": user_input}],
29
+ model=MODEL_NAME,
30
+ )
31
+ return response.choices[0].message.content
32
+ except Exception as e:
33
+ return f"Error from Groq: {e}"
34
+
35
+ # === Chatbot logic ===
36
  def chat(user_input, chat_history, image):
37
  try:
38
+ ai_reply = generate_response(user_input)
39
+
40
  if image is not None:
41
  image_description = describe_image(image)
42
+ ai_reply += f"\n\n[Image Description]: {image_description}"
43
+
44
  chat_history.append(("User", user_input))
45
+ chat_history.append(("AI", ai_reply))
46
+
47
+ formatted = "\n".join([f"{role}: {msg}" for role, msg in chat_history])
48
+ return formatted, chat_history
49
  except Exception as e:
50
  return f"Error: {e}", chat_history
51
 
52
+ # === Gradio Interface ===
53
  with gr.Blocks(css="""
 
 
 
 
 
54
  .gradio-container {
55
+ font-family: 'Segoe UI', sans-serif;
56
+ background-color: #f5f5f5;
57
+ padding: 20px;
 
 
 
 
 
 
 
58
  }
59
  #chatbox {
60
  height: 300px;
61
  overflow-y: auto;
62
+ background-color: #ffffff;
63
+ border: 1px solid #ccc;
64
  border-radius: 10px;
65
+ padding: 15px;
 
66
  font-size: 14px;
67
+ line-height: 1.5;
68
  }
69
  """) as demo:
70
+
71
+ gr.Markdown("## 🤖 **Groq-powered Chatbot with Image Understanding (BLIP)**")
72
+ gr.Markdown("Chat with the bot or upload an image to get a caption.")
73
 
74
  with gr.Column():
75
+ user_input = gr.Textbox(label="Your Message", placeholder="Ask something...", lines=2)
76
+ submit_button = gr.Button("Send")
77
  clear_button = gr.Button("Clear Chat")
78
+
79
  chatbot_output = gr.Textbox(label="Chat History", lines=12, interactive=False, elem_id="chatbox")
80
 
81
+ image_input = gr.Image(label="Upload an Image", type="pil", elem_id="image-upload")
82
  upload_button = gr.Button("Describe Image")
83
  image_caption = gr.Textbox(label="Image Description", interactive=False)
84
 
 
88
  clear_button.click(fn=lambda: ("", []), inputs=[], outputs=[chatbot_output, chat_history])
89
  upload_button.click(fn=describe_image, inputs=[image_input], outputs=[image_caption])
90
 
91
+ # === Launch the app ===
92
  if __name__ == "__main__":
93
  demo.launch()