File size: 3,719 Bytes
1ec618e
 
 
 
fa84049
 
1ec618e
fa84049
 
1ec618e
 
 
 
 
 
fa84049
 
 
 
 
 
1ec618e
fa84049
1ec618e
 
fa84049
1ec618e
 
 
 
 
fa84049
1ec618e
fa84049
1ec618e
fa84049
 
 
1ec618e
fa84049
 
 
1ec618e
fa84049
90d6e06
1ec618e
fa84049
1ec618e
fa84049
 
1ec618e
fa84049
 
1ec618e
fa84049
1ec618e
 
fa84049
 
 
1ec618e
fa84049
1ec618e
 
 
fa84049
1ec618e
 
fa84049
1ec618e
ea77364
1ec618e
 
 
 
fa84049
 
1ec618e
 
fa84049
1ec618e
 
fa84049
1ec618e
 
fa84049
 
1ec618e
 
 
fa84049
1ec618e
 
fa84049
 
 
1ec618e
fa84049
1ec618e
 
fa84049
1ec618e
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Importing necessary Python libraries (pre-made code that helps us do specific tasks)
import os  # This helps us access environment variables, like secret keys
import gradio as gr  # Gradio helps us build simple web interfaces
import openai  # This allows us to connect to Poe’s API (powered by OpenAI)

# -------------------------
# 🛠️ Poe API Setup
# -------------------------

# We get the Poe API key from the environment (it's stored securely in Hugging Face Secrets)
# This key lets us connect to Poe’s chatbot service
POE_API_KEY = os.environ["POE_API_KEY"]

# Creating a client (connection) to the Poe API
# `base_url` is where the Poe service lives online
client = openai.OpenAI(
    api_key=POE_API_KEY,
    base_url="https://api.poe.com/v1"
)

# -------------------------
# 💬 Chat Function
# -------------------------

# This is the main function that handles the conversation with Poe
def chat_with_poe(history, message):
    # `history` stores past messages between user and bot
    # `message` is the new user message that needs a reply

    # Convert Gradio’s format to Poe’s format
    messages = []  # This will be a list of messages we send to Poe
    for user_msg, bot_msg in history:
        # Add the user’s previous message
        messages.append({"role": "user", "content": user_msg})
        # If there's a bot reply, add it too
        if bot_msg:
            messages.append({"role": "assistant", "content": bot_msg})

    # Add the new user message to the end of the list
    messages.append({"role": "user", "content": message})

    try:
        # Send the messages to Poe and get the response from the chatbot
        response = client.chat.completions.create(
            model="My_Tutor_Shq",  # The name of your bot on Poe
            messages=messages  # All past and current messages
        )
        # Extract the bot’s reply from the response
        bot_reply = response.choices[0].message.content
    except Exception as e:
        # If something goes wrong, show an error message
        bot_reply = f"⚠️ Error: {str(e)}"

    # Add the new message and bot reply to the chat history
    history.append((message, bot_reply))

    # Return updated chat history twice (Gradio expects it this way)
    return history, history

# -------------------------
# 🖼️ Gradio Chat Interface (UI)
# -------------------------

# `with gr.Blocks()` sets up a visual layout for our chat interface
# `theme=gr.themes.Soft()` makes the interface look nice and clean
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    
    # This adds a title and description at the top
    gr.Markdown(

        # 🤖 Your title goes here
        # Description 
        # Type below to start chatting.
        """
        # 🤖 Python Tutor Bot
        """
    )
    
    # This creates the actual chatbot display window
    chatbot = gr.Chatbot([], height=400)
    
    # A text box where the user can type their message
    msg = gr.Textbox(placeholder="Type your message...")

    # A button to clear the chat history
    clear = gr.Button("Clear Chat")

    # When the user presses enter in the textbox:
    # Call `chat_with_poe()` using the current chat and new message
    # Update the chat window with the new messages
    msg.submit(chat_with_poe, [chatbot, msg], [chatbot, chatbot])

    # When the "Clear Chat" button is clicked, reset the chatbot window to empty
    clear.click(lambda: [], None, chatbot)

# -------------------------
# 🚀 Launch the App
# -------------------------

# This runs the app if the file is being executed directly (not imported)
if __name__ == "__main__":
    demo.launch()  # Starts the Gradio interface so users can chat with the bot