Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesFiles changed (2)
- app.pyAdded+103
- requirements.txtAdded+2
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103# Importing necessary Python libraries (pre-made code that helps us do specific tasks)import os # This helps us access environment variables, like secret keysimport gradio as gr # Gradio helps us build simple web interfacesimport 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 servicePOE_API_KEY = os.environ["POE_API_KEY"]
# Creating a client (connection) to the Poe API# `base_url` is where the Poe service lives onlineclient = 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 Poedef 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="Shq_Test", # 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 cleanwith 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
12gradioopenaiapp.pyAdded+103
requirements.txtAdded+2