|
|
import gradio as gr |
|
|
import google.generativeai as genai |
|
|
import os |
|
|
|
|
|
|
|
|
GOOGLE_API_KEY = "AIzaSyD4qTdgk3IYPNRH8kgbxSYAOkdOupt0rkk" |
|
|
genai.configure(api_key=GOOGLE_API_KEY) |
|
|
|
|
|
|
|
|
AI_NAME = "Lan π" |
|
|
user_data = {"name": None, "age": None, "city": None, "likes": None, "favorites": None} |
|
|
|
|
|
|
|
|
def chat_with_ai(user_input, history=[]): |
|
|
global AI_NAME, user_data |
|
|
|
|
|
|
|
|
if "your name" in user_input.lower(): |
|
|
return f"My name is {AI_NAME}! π" |
|
|
|
|
|
|
|
|
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! π" |
|
|
|
|
|
|
|
|
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']}! π‘" |
|
|
|
|
|
|
|
|
response = genai.generate_text(prompt=user_input).result |
|
|
|
|
|
return response |
|
|
|
|
|
|
|
|
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]) |
|
|
|
|
|
|
|
|
app.launch() |
|
|
|