Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,42 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from openai import OpenAI
|
| 5 |
|
| 6 |
-
# 🔑 API
|
| 7 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
| 9 |
|
| 10 |
-
# Баталгаажуулах
|
| 11 |
print("OPENAI_API_KEY =", openai_api_key)
|
| 12 |
print("GEMINI_API_KEY =", gemini_api_key)
|
| 13 |
|
| 14 |
-
# OpenAI client
|
| 15 |
if openai_api_key is None:
|
| 16 |
raise ValueError("⚠️ OPENAI_API_KEY тохируулна уу!")
|
|
|
|
| 17 |
client = OpenAI(api_key=openai_api_key)
|
| 18 |
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def chatgpt_response(prompt):
|
| 21 |
try:
|
| 22 |
-
response = client.responses.create(
|
| 23 |
-
model="gpt-5",
|
| 24 |
-
input=prompt
|
| 25 |
-
)
|
| 26 |
return response.output_text
|
| 27 |
except Exception as e:
|
| 28 |
return f"⚠️ ChatGPT error: {e}"
|
| 29 |
|
| 30 |
-
# Gemini
|
| 31 |
def gemini_response(prompt):
|
| 32 |
if gemini_api_key is None:
|
| 33 |
return "⚠️ GEMINI_API_KEY тохируулаагүй байна!"
|
|
@@ -41,26 +50,25 @@ def gemini_response(prompt):
|
|
| 41 |
except Exception as e:
|
| 42 |
return f"⚠️ Gemini error: {e}"
|
| 43 |
|
| 44 |
-
# Fusion
|
| 45 |
-
def fusion_ai(prompt):
|
| 46 |
gpt_reply = chatgpt_response(prompt)
|
| 47 |
gemini_reply = gemini_response(prompt)
|
| 48 |
|
| 49 |
if "ChatGPT error" in gpt_reply.lower():
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
<p><b>ChatGPT 🧠:</b> {gpt_reply}</p>
|
| 56 |
-
<p><b>Gemini 🌐:</b> {gemini_reply}</p>
|
| 57 |
-
<hr style="border:1px solid #222;">
|
| 58 |
-
<h4>✨ <span style="color:#9b59b6">ZeppFusion Combined Insight:</span></h4>
|
| 59 |
-
<p>{gpt_reply if len(gpt_reply) > len(gemini_reply) else gemini_reply}</p>
|
| 60 |
-
</div>
|
| 61 |
-
"""
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
css = """
|
| 65 |
.gradio-container {background-color: #0d1117 !important; color: #e6edf3;}
|
| 66 |
h1, h3, h4, label {color: #00d9ff !important;}
|
|
@@ -68,15 +76,21 @@ textarea, input {background-color:#161b22 !important; color:#fff !important; bor
|
|
| 68 |
button {background-color:#00d9ff !important; color:#000 !important; font-weight:bold; border-radius:10px;}
|
| 69 |
"""
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
app.launch()
|
|
|
|
| 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 тохируулаагүй байна!"
|
|
|
|
| 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;}
|
|
|
|
| 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()
|