Shafaq25 commited on
Commit
1ec618e
·
verified ·
1 Parent(s): 48df11b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -16
app.py CHANGED
@@ -1,62 +1,103 @@
1
- import os
2
- import gradio as gr
3
- import openai
 
4
 
5
  # -------------------------
6
- # Poe API Setup
7
  # -------------------------
8
- POE_API_KEY = os.environ["POE_API_KEY"] # Stored in HF Spaces Secrets
9
 
 
 
 
 
 
 
10
  client = openai.OpenAI(
11
  api_key=POE_API_KEY,
12
  base_url="https://api.poe.com/v1"
13
  )
14
 
15
  # -------------------------
16
- # Chat function - Explain this - By Alisha
17
  # -------------------------
 
 
18
  def chat_with_poe(history, message):
19
- # Convert Gradio's history to Poe's format
20
- messages = []
 
 
 
21
  for user_msg, bot_msg in history:
 
22
  messages.append({"role": "user", "content": user_msg})
 
23
  if bot_msg:
24
  messages.append({"role": "assistant", "content": bot_msg})
25
 
 
26
  messages.append({"role": "user", "content": message})
27
 
28
  try:
 
29
  response = client.chat.completions.create(
30
- model="TechBuddyAI", # Your Poe bot name
31
- messages=messages
32
  )
 
33
  bot_reply = response.choices[0].message.content
34
  except Exception as e:
 
35
  bot_reply = f"⚠️ Error: {str(e)}"
36
 
 
37
  history.append((message, bot_reply))
 
 
38
  return history, history
39
 
40
  # -------------------------
41
- # Gradio Chat UI
42
  # -------------------------
 
 
 
43
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
 
44
  gr.Markdown(
45
- """
46
  # 🤖 Your title goes here
47
- Description
48
- Type below to start chatting.
 
 
49
  """
50
  )
 
 
51
  chatbot = gr.Chatbot([], height=400)
 
 
52
  msg = gr.Textbox(placeholder="Type your message...")
 
 
53
  clear = gr.Button("Clear Chat")
54
 
 
 
 
55
  msg.submit(chat_with_poe, [chatbot, msg], [chatbot, chatbot])
 
 
56
  clear.click(lambda: [], None, chatbot)
57
 
58
  # -------------------------
59
- # Launch App
60
  # -------------------------
 
 
61
  if __name__ == "__main__":
62
- demo.launch()
 
 
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="TechBuddyAI", # 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
+