Spaces:
Sleeping
Sleeping
| import os | |
| from groq import Groq | |
| import gradio as gr | |
| from PIL import Image | |
| from transformers import BlipProcessor, BlipForConditionalGeneration | |
| # Retrieve Groq API Key securely from environment variables | |
| api_key = os.getenv("GROQ_API_KEY") | |
| if not api_key: | |
| raise ValueError("GROQ_API_KEY not found. Please add it to your environment variables.") | |
| # Initialize Groq Client | |
| client = Groq(api_key=api_key) | |
| # Initialize BLIP model for image captioning | |
| processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") | |
| model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base") | |
| # Function to process image and generate caption | |
| def process_image(image): | |
| inputs = processor(image, return_tensors="pt") | |
| outputs = model.generate(**inputs) | |
| caption = processor.decode(outputs[0], skip_special_tokens=True) | |
| return caption | |
| # Function to generate chatbot responses with text and image | |
| def chatbot_response(user_input, image=None, chat_history=[]): | |
| try: | |
| # Process image if provided | |
| image_caption = "" | |
| if image: | |
| image_caption = process_image(image) | |
| # Prepare input messages for Groq | |
| combined_input = user_input | |
| if image_caption: | |
| combined_input += f"\n\nImage Caption: {image_caption}" | |
| messages = [{"role": "user", "content": combined_input}] | |
| # Generate response from Groq API | |
| chat_completion = client.chat.completions.create( | |
| messages=messages, | |
| model="llama-3.3-70b-versatile", | |
| stream=False, | |
| ) | |
| response = chat_completion.choices[0].message.content | |
| # Update chat history | |
| chat_history.append({"role": "user", "content": user_input}) | |
| if image_caption: | |
| chat_history.append({"role": "user", "content": f"Image Caption: {image_caption}"}) | |
| chat_history.append({"role": "assistant", "content": response}) | |
| return chat_history, chat_history | |
| except Exception as e: | |
| return chat_history + [{"role": "assistant", "content": f"Error: {str(e)}"}], chat_history | |
| # Create Gradio Interface with enhanced UI | |
| with gr.Blocks(title="Chatbot with Text and Image Input") as interface: | |
| gr.Markdown( | |
| """ | |
| # π€ **Chatbot with Text and Image Input** | |
| Powered by **Groq** and enhanced with **BLIP Image Captioning**. | |
| π *Type your message below, attach a photo (optional), and get a response!* | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=4): | |
| chatbot = gr.Chatbot(label="Chat", type="messages") | |
| with gr.Column(scale=1): | |
| gr.Markdown( | |
| """ | |
| ## Instructions | |
| 1. Enter your message in the text box. | |
| 2. Attach an image if needed. | |
| 3. Click 'Send' or press 'Enter' to chat. | |
| 4. Enjoy your enhanced conversation! π | |
| """ | |
| ) | |
| user_input = gr.Textbox( | |
| label="Your Message", | |
| placeholder="Type your message here...", | |
| lines=1, | |
| ) | |
| user_image = gr.Image( | |
| label="Attach Image (Optional)", | |
| type="pil", | |
| ) | |
| submit_button = gr.Button("Send π") | |
| clear_button = gr.Button("Clear Chat π§Ή") | |
| chat_history = gr.State([]) # Initialize chat history | |
| # Define interaction logic | |
| submit_button.click( | |
| chatbot_response, | |
| inputs=[user_input, user_image, chat_history], | |
| outputs=[chatbot, chat_history], | |
| ) | |
| clear_button.click( | |
| lambda: ([], []), | |
| inputs=None, | |
| outputs=[chatbot, chat_history], | |
| ) | |
| # Launch Gradio App | |
| if __name__ == "__main__": | |
| interface.launch() | |