File size: 2,591 Bytes
c3c51ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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()