Xyro123 commited on
Commit
e632292
·
verified ·
1 Parent(s): a9c0001

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -62
app.py CHANGED
@@ -1,45 +1,14 @@
1
  import os
2
  import requests
3
  import gradio as gr
4
- import sqlite3
5
- from openai import OpenAI
6
 
7
- # 🔑 API keys
8
- openai_api_key = os.getenv("OPENAI_API_KEY")
9
  gemini_api_key = os.getenv("GEMINI_API_KEY")
 
 
10
 
11
- print("OPENAI_API_KEY =", openai_api_key)
12
- print("GEMINI_API_KEY =", gemini_api_key)
13
-
14
- if openai_api_key is None:
15
- raise ValueError("⚠️ OPENAI_API_KEY тохируулна уу!")
16
-
17
- client = OpenAI(api_key=openai_api_key)
18
-
19
- # SQLite db setup
20
- conn = sqlite3.connect("chat_history.db", check_same_thread=False)
21
- c = conn.cursor()
22
- c.execute("""
23
- CREATE TABLE IF NOT EXISTS history (
24
- id INTEGER PRIMARY KEY AUTOINCREMENT,
25
- user_input TEXT,
26
- fusion_output TEXT
27
- )
28
- """)
29
- conn.commit()
30
-
31
- # ChatGPT (gpt-5)
32
- def chatgpt_response(prompt):
33
- try:
34
- response = client.responses.create(model="gpt-5", input=prompt)
35
- return response.output_text
36
- except Exception as e:
37
- return f"⚠️ ChatGPT error: {e}"
38
-
39
- # Gemini
40
  def gemini_response(prompt):
41
- if gemini_api_key is None:
42
- return "⚠️ GEMINI_API_KEY тохируулаагүй байна!"
43
  try:
44
  url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={gemini_api_key}"
45
  headers = {"Content-Type": "application/json"}
@@ -50,47 +19,33 @@ def gemini_response(prompt):
50
  except Exception as e:
51
  return f"⚠️ Gemini error: {e}"
52
 
53
- # Fusion + summary
54
- def fusion_ai(prompt, chat_history=[]):
55
- gpt_reply = chatgpt_response(prompt)
56
- gemini_reply = gemini_response(prompt)
57
-
58
- if "ChatGPT error" in gpt_reply.lower():
59
- combined = f"⚠️ ChatGPT quota дууссан тул Gemini хариу:\n\n{gemini_reply}"
60
- else:
61
- # Fusion summary: урт хариултаас сонгох
62
- combined = f"ChatGPT 🧠: {gpt_reply}\nGemini 🌐: {gemini_reply}"
63
-
64
- # Update SQLite history
65
- c.execute("INSERT INTO history (user_input, fusion_output) VALUES (?, ?)", (prompt, combined))
66
- conn.commit()
67
 
68
- chat_history.append((prompt, combined))
69
- return chat_history, chat_history
70
-
71
- # Gradio interface
72
  css = """
73
  .gradio-container {background-color: #0d1117 !important; color: #e6edf3;}
74
  h1, h3, h4, label {color: #00d9ff !important;}
75
  textarea, input {background-color:#161b22 !important; color:#fff !important; border-radius:10px;}
76
  button {background-color:#00d9ff !important; color:#000 !important; font-weight:bold; border-radius:10px;}
 
 
77
  """
78
 
 
79
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as app:
80
- gr.Markdown("## 🚀 ZeppFusion AI — Persistent Fusion Chat")
81
  chatbot = gr.Chatbot()
82
  msg = gr.Textbox(placeholder="Таны асуулт...")
83
  clear = gr.Button("🗑️ Clear History")
84
 
85
- # Submit: add to history
86
- msg.submit(fusion_ai, [msg, chatbot], [chatbot, chatbot])
87
 
88
- # Clear button: delete SQLite history
89
- def clear_history():
90
- c.execute("DELETE FROM history")
91
- conn.commit()
92
- return []
93
- clear.click(clear_history, None, chatbot)
94
 
95
  if __name__ == "__main__":
96
  app.launch()
 
1
  import os
2
  import requests
3
  import gradio as gr
 
 
4
 
5
+ # Gemini API key
 
6
  gemini_api_key = os.getenv("GEMINI_API_KEY")
7
+ if gemini_api_key is None:
8
+ raise ValueError("⚠️ GEMINI_API_KEY тохируулаагүй байна!")
9
 
10
+ # Gemini response function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def gemini_response(prompt):
 
 
12
  try:
13
  url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={gemini_api_key}"
14
  headers = {"Content-Type": "application/json"}
 
19
  except Exception as e:
20
  return f"⚠️ Gemini error: {e}"
21
 
22
+ # Chat function with history
23
+ def chat(prompt, history=[]):
24
+ reply = gemini_response(prompt)
25
+ history.append((prompt, reply))
26
+ return history, history
 
 
 
 
 
 
 
 
 
27
 
28
+ # Custom CSS for chat design
 
 
 
29
  css = """
30
  .gradio-container {background-color: #0d1117 !important; color: #e6edf3;}
31
  h1, h3, h4, label {color: #00d9ff !important;}
32
  textarea, input {background-color:#161b22 !important; color:#fff !important; border-radius:10px;}
33
  button {background-color:#00d9ff !important; color:#000 !important; font-weight:bold; border-radius:10px;}
34
+ .gradio-chatbot-message.user {background-color:#0f2c54; color:#fff; border-radius:15px; padding:10px;}
35
+ .gradio-chatbot-message.bot {background-color:#1f1f1f; color:#00d9ff; border-radius:15px; padding:10px;}
36
  """
37
 
38
+ # Gradio interface
39
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as app:
40
+ gr.Markdown("## 🚀 ZeppFusion AI — Gemini Chat Only")
41
  chatbot = gr.Chatbot()
42
  msg = gr.Textbox(placeholder="Таны асуулт...")
43
  clear = gr.Button("🗑️ Clear History")
44
 
45
+ msg.submit(chat, [msg, chatbot], [chatbot, chatbot])
 
46
 
47
+ # Clear button
48
+ clear.click(lambda: [], None, chatbot)
 
 
 
 
49
 
50
  if __name__ == "__main__":
51
  app.launch()