Echoforge24 commited on
Commit
758a6c9
·
verified ·
1 Parent(s): 2e85fbb

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing necessary Python libraries (pre-made code that helps us do specific tasks)
2
+ import os # This helps us access environment variables, like secret keys
3
+ import gradio as gr # Gradio helps us build simple web interfaces
4
+ import openai # This allows us to connect to Poe’s API (powered by OpenAI)
5
+
6
+ # -------------------------
7
+ # 🛠️ Poe API Setup
8
+ # -------------------------
9
+
10
+ # We get the Poe API key from the environment (it's stored securely in Hugging Face Secrets)
11
+ # This key lets us connect to Poe’s chatbot service
12
+ POE_API_KEY = os.environ["POE_API_KEY"]
13
+
14
+ # Creating a client (connection) to the Poe API
15
+ # `base_url` is where the Poe service lives online
16
+ client = openai.OpenAI(
17
+ api_key=POE_API_KEY,
18
+ base_url="https://api.poe.com/v1"
19
+ )
20
+
21
+ # -------------------------
22
+ # 💬 Chat Function
23
+ # -------------------------
24
+
25
+ # This is the main function that handles the conversation with Poe
26
+ def chat_with_poe(history, message):
27
+ # `history` stores past messages between user and bot
28
+ # `message` is the new user message that needs a reply
29
+
30
+ # Convert Gradio’s format to Poe’s format
31
+ messages = [] # This will be a list of messages we send to Poe
32
+ for user_msg, bot_msg in history:
33
+ # Add the user’s previous message
34
+ messages.append({"role": "user", "content": user_msg})
35
+ # If there's a bot reply, add it too
36
+ if bot_msg:
37
+ messages.append({"role": "assistant", "content": bot_msg})
38
+
39
+ # Add the new user message to the end of the list
40
+ messages.append({"role": "user", "content": message})
41
+
42
+ try:
43
+ # Send the messages to Poe and get the response from the chatbot
44
+ response = client.chat.completions.create(
45
+ model="My_Tutor_Shq", # The name of your bot on Poe
46
+ messages=messages # All past and current messages
47
+ )
48
+ # Extract the bot’s reply from the response
49
+ bot_reply = response.choices[0].message.content
50
+ except Exception as e:
51
+ # If something goes wrong, show an error message
52
+ bot_reply = f"⚠️ Error: {str(e)}"
53
+
54
+ # Add the new message and bot reply to the chat history
55
+ history.append((message, bot_reply))
56
+
57
+ # Return updated chat history twice (Gradio expects it this way)
58
+ return history, history
59
+
60
+ # -------------------------
61
+ # 🖼️ Gradio Chat Interface (UI)
62
+ # -------------------------
63
+
64
+ # `with gr.Blocks()` sets up a visual layout for our chat interface
65
+ # `theme=gr.themes.Soft()` makes the interface look nice and clean
66
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
67
+
68
+ # This adds a title and description at the top
69
+ gr.Markdown(
70
+
71
+ # 🤖 Your title goes here
72
+ # Description
73
+ # Type below to start chatting.
74
+ """
75
+ # 🤖 Python Tutor Bot
76
+ """
77
+ )
78
+
79
+ # This creates the actual chatbot display window
80
+ chatbot = gr.Chatbot([], height=400)
81
+
82
+ # A text box where the user can type their message
83
+ msg = gr.Textbox(placeholder="Type your message...")
84
+
85
+ # A button to clear the chat history
86
+ clear = gr.Button("Clear Chat")
87
+
88
+ # When the user presses enter in the textbox:
89
+ # Call `chat_with_poe()` using the current chat and new message
90
+ # Update the chat window with the new messages
91
+ msg.submit(chat_with_poe, [chatbot, msg], [chatbot, chatbot])
92
+
93
+ # When the "Clear Chat" button is clicked, reset the chatbot window to empty
94
+ clear.click(lambda: [], None, chatbot)
95
+
96
+ # -------------------------
97
+ # 🚀 Launch the App
98
+ # -------------------------
99
+
100
+ # This runs the app if the file is being executed directly (not imported)
101
+ if __name__ == "__main__":
102
+ demo.launch() # Starts the Gradio interface so users can chat with the bot
103
+