import gradio as gr import google.generativeai as genai import os # Set up Google Gemini API (replace with your own API key) GOOGLE_API_KEY = "AIzaSyD4qTdgk3IYPNRH8kgbxSYAOkdOupt0rkk" genai.configure(api_key=GOOGLE_API_KEY) # Initialize AI's constant details AI_NAME = "Lan 💖" user_data = {"name": None, "age": None, "city": None, "likes": None, "favorites": None} # Function to generate responses using Google Gemini API def chat_with_ai(user_input, history=[]): global AI_NAME, user_data # Handle name-related questions if "your name" in user_input.lower(): return f"My name is {AI_NAME}! 😊" # Extract user details if they share if "my name is" in user_input.lower(): user_data["name"] = user_input.split("my name is")[-1].strip(" .!") return f"Nice to meet you, {user_data['name']}! 😊" if "i am" in user_input.lower() and "years old" in user_input.lower(): user_data["age"] = "".join(filter(str.isdigit, user_input)) return f"Got it! You're {user_data['age']} years old. 🎂" if "i live in" in user_input.lower(): user_data["city"] = user_input.split("i live in")[-1].strip(" .!") return f"Oh cool! {user_data['city']} must be a great place! 🌍" # Remember user details if "what's my name" in user_input.lower() and user_data["name"]: return f"Your name is {user_data['name']}! 😃" if "how old am i" in user_input.lower() and user_data["age"]: return f"You're {user_data['age']} years old! 🎉" if "where do i live" in user_input.lower() and user_data["city"]: return f"You live in {user_data['city']}! 🏡" # Generate response using Google Gemini API response = genai.generate_text(prompt=user_input).result return response # Gradio UI with an attractive theme with gr.Blocks(theme=gr.themes.Soft()) as app: gr.Markdown("

💖 AI Friend - Lan! 💖

") chatbot = gr.Chatbot(label="Lan 💖 - Your AI Friend", bubble_full_width=False) with gr.Row(): user_input = gr.Textbox(placeholder="Type something...", label="You 💬", show_label=False) send_btn = gr.Button("💬 Send") def respond(message, history): response = chat_with_ai(message) history.append((message, response)) return history, "" send_btn.click(respond, inputs=[user_input, chatbot], outputs=[chatbot, user_input]) user_input.submit(respond, inputs=[user_input, chatbot], outputs=[chatbot, user_input]) # Launch the chatbot app.launch()