Download app.py from mukilan-k/letschat: direct link, hf CLI and curl.
- Browser
- Download file 2.59 kB
-
https://huggingface.co/spaces/mukilan-k/letschat/resolve/main/app.py
- Command line
-
hf download hf://spaces/mukilan-k/letschat/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mukilan-k/letschat/resolve/main/app.py
2.59 kB
| 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("<h1 style='text-align: center; color: pink;'>π AI Friend - Lan! π</h1>") | |
| 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() | |