Echoforge24 commited on
Commit
516bd3e
·
verified ·
1 Parent(s): 545a19f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -15
app.py CHANGED
@@ -1,38 +1,71 @@
 
1
  import os
2
  import gradio as gr
3
- import google.generativeai as genai
4
 
5
- GOOGLE_API_KEY = os.environ.get("1M-vj81pjfDeddkHOtcovh-NZb_ltZJLNojrm_Px5oI")
 
 
6
 
7
- if not GOOGLE_API_KEY:
8
- raise ValueError("GOOGLE_API_KEY is missing. Add it in HF Secrets.")
9
 
10
- genai.configure(api_key=GOOGLE_API_KEY)
 
11
 
12
- def chat_with_gemini(history, message):
13
- conversation = ""
 
 
 
 
 
 
 
 
 
 
 
 
14
  for user_msg, bot_msg in history:
15
- conversation += f"User: {user_msg}\nAssistant: {bot_msg}\n"
 
 
16
 
17
- conversation += f"User: {message}\nAssistant:"
 
18
 
19
  try:
20
- model = genai.GenerativeModel("gemini-1.5-flash")
21
- response = model.generate_content(conversation)
22
- bot_reply = response.text.strip()
 
 
23
  except Exception as e:
24
  bot_reply = f"⚠️ Error: {e}"
25
 
26
  history.append((message, bot_reply))
27
  return history, history
28
 
 
 
 
 
29
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
30
- gr.Markdown("# 🤖 EchoForge Chatbot")
 
31
  chatbot = gr.Chatbot([], height=400)
32
- msg = gr.Textbox(placeholder="Type your message...")
 
 
 
33
  clear = gr.Button("Clear Chat")
34
 
35
- msg.submit(chat_with_gemini, [chatbot, msg], [chatbot, chatbot])
36
  clear.click(lambda: [], outputs=[chatbot])
37
 
 
 
 
 
38
  demo.launch()
 
1
+ # Import required libraries
2
  import os
3
  import gradio as gr
4
+ from openai import OpenAI
5
 
6
+ # -------------------------
7
+ # 🛠️ Poe API Setup
8
+ # -------------------------
9
 
10
+ # Get Poe API key from Hugging Face Secrets
11
+ POE_API_KEY = os.environ.get("1M-vj81pjfDeddkHOtcovh-NZb_ltZJLNojrm_Px5oI")
12
 
13
+ if not POE_API_KEY:
14
+ raise ValueError("POE_API_KEY is missing. Add it in HF Spaces → Settings → Secrets.")
15
 
16
+ # Create Poe client (Poe uses OpenAI-compatible API)
17
+ client = OpenAI(
18
+ api_key=POE_API_KEY,
19
+ base_url="https://api.poe.com/v1"
20
+ )
21
+
22
+ # -------------------------
23
+ # 💬 Chat Function
24
+ # -------------------------
25
+
26
+ def chat_with_poe(history, message):
27
+ messages = []
28
+
29
+ # Convert Gradio history to Poe / OpenAI format
30
  for user_msg, bot_msg in history:
31
+ messages.append({"role": "user", "content": user_msg})
32
+ if bot_msg:
33
+ messages.append({"role": "assistant", "content": bot_msg})
34
 
35
+ # Add new user message
36
+ messages.append({"role": "user", "content": message})
37
 
38
  try:
39
+ response = client.chat.completions.create(
40
+ model="EchoForge_20", # EXACT Poe bot name
41
+ messages=messages
42
+ )
43
+ bot_reply = response.choices[0].message.content.strip()
44
  except Exception as e:
45
  bot_reply = f"⚠️ Error: {e}"
46
 
47
  history.append((message, bot_reply))
48
  return history, history
49
 
50
+ # -------------------------
51
+ # 🖼️ Gradio UI
52
+ # -------------------------
53
+
54
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
55
+ gr.Markdown("# 🤖 Python Tutor Bot")
56
+
57
  chatbot = gr.Chatbot([], height=400)
58
+ msg = gr.Textbox(
59
+ placeholder="Type your message and press Enter...",
60
+ show_label=False
61
+ )
62
  clear = gr.Button("Clear Chat")
63
 
64
+ msg.submit(chat_with_poe, [chatbot, msg], [chatbot, chatbot])
65
  clear.click(lambda: [], outputs=[chatbot])
66
 
67
+ # -------------------------
68
+ # 🚀 Launch App
69
+ # -------------------------
70
+
71
  demo.launch()