import os import gradio as gr import requests import random from transformers import AutoTokenizer, AutoModelForCausalLM import speech_recognition as sr from groq import Groq client = Groq( api_key=os.getenv('GROQ_API_KEY'), ) # Predefined list of jokes jokes = [ "Why don't scientists trust atoms? Because they make up everything!", "Why did the scarecrow win an award? Because he was outstanding in his field!", "Why don't skeletons fight each other? They don't have the guts." ] # Function to fetch weather data from wttr.in def get_weather(city): base_url = f"http://wttr.in/{city}?format=%C+%t+%w+%m" response = requests.get(base_url) if response.status_code == 200: return response.text.strip() else: return "City not found." def chat_with_groq(input_text): chat_completion = client.chat.completions.create( messages=[ { "role": "user", "content": input_text, } ], model="llama-3.1-8b-instant", ) return chat_completion.choices[0].message.content # Function to generate chatbot response def chatbot_response(input_text): if "tell me a joke" in input_text.lower(): response = random.choice(jokes) elif "weather in" in input_text.lower(): city = input_text.split("weather in")[-1].strip() response = get_weather(city) else: response = chat_with_groq(input_text) return response # Function to handle image input def image_response(image): response = "I see you've uploaded an image. It looks interesting!" return response # Function to handle audio input def audio_response(audio): r = sr.Recognizer() with sr.AudioFile(audio) as source: audio_text = r.recognize_google(r.record(source)) response = f"I heard: {audio_text}" return response # Function to handle drawing input def drawing_response(drawing): response = "Thanks for the drawing! It's a creative piece of art." return response # Custom CSS for a colorful, modern chat UI with 3D buttons custom_css = """ body { background-color: #e6f3ff; font-family: 'Arial', sans-serif; } .gradio-container { max-width: 800px; margin: 50px auto; padding: 20px; border-radius: 20px; background-color: #ffffff; box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); } #chatbot { height: 400px; overflow-y: auto; border: 2px solid #007bff; border-radius: 15px; padding: 10px; background-color: #f8f9fa; } .gr-chatbot-message { padding: 10px 20px; border-radius: 25px; margin: 5px 0; display: inline-block; max-width: 80%; } .gr-chatbot-message.user { background-color: #007bff; color: white; align-self: flex-end; } .gr-chatbot-message.bot { background-color: #28a745; color: white; } .gr-button { background-color: #17a2b8; border: none; color: white; border-radius: 25px; padding: 10px 20px; margin: 10px 5px; width: auto; font-size: 16px; box-shadow: 0 4px #138496; transition: 0.3s; } .gr-button:hover { background-color: #138496; box-shadow: 0 6px #0f6674; } .gr-button:active { background-color: #117a8b; box-shadow: 0 2px #0e6070; transform: translateY(2px); } .gr-form { background-color: #f8f9fa; border: 2px solid #dee2e6; border-radius: 15px; padding: 20px; margin-top: 20px; } #input_box, .gr-image, .gr-audio, .gr-sketchpad { background-color: #ffffff; border: 1px solid #ced4da; border-radius: 15px; padding: 10px 15px; margin: 10px 0; width: 100%; font-size: 16px; color: #495057; } """ # Create the Gradio interface with gr.Blocks(css=custom_css) as demo: gr.Markdown("# 🤖 Chat with GPT") chatbot_ui = gr.Chatbot(label="Chatbot", elem_id="chatbot") with gr.Row(): input_box = gr.Textbox(show_label=False, placeholder="Type a message...", elem_id="input_box") submit_button = gr.Button("Send", elem_id="submit_button") with gr.Accordion("Additional Inputs"): with gr.Row(): image_input = gr.Image(type="filepath", label="Upload an image") image_button = gr.Button("Send Image", elem_id="image_button") with gr.Row(): audio_input = gr.Audio(type="filepath", label="Record audio") audio_button = gr.Button("Send Audio", elem_id="audio_button") with gr.Row(): drawing_input = gr.Sketchpad(label="Draw something") drawing_button = gr.Button("Send Drawing", elem_id="drawing_button") def respond(message): response = chatbot_response(message) return "", response def respond_image(image): response = image_response(image) return response def respond_audio(audio): response = audio_response(audio) return response def respond_drawing(drawing): response = drawing_response(drawing) return response submit_button.click(respond, [input_box], [input_box, chatbot_ui]) image_button.click(respond_image, [image_input], [chatbot_ui]) audio_button.click(respond_audio, [audio_input], [chatbot_ui]) drawing_button.click(respond_drawing, [drawing_input], [chatbot_ui]) demo.launch()