Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,80 @@
|
|
| 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
|
| 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"}
|
| 15 |
-
data = {"contents":
|
| 16 |
res = requests.post(url, headers=headers, json=data)
|
| 17 |
res_json = res.json()
|
| 18 |
return res_json["candidates"][0]["content"]["parts"][0]["text"]
|
| 19 |
except Exception as e:
|
| 20 |
return f"⚠️ Gemini error: {e}"
|
| 21 |
|
| 22 |
-
# Chat function with
|
| 23 |
-
def chat(prompt, history=[]):
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
return history, history
|
| 27 |
|
| 28 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
css = """
|
| 30 |
-
.gradio-container {
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
.gradio-chatbot-message.user {
|
| 35 |
-
.gradio-chatbot-message.bot {background-color:#1f1f1f; color:#00d9ff; border-radius:
|
| 36 |
-
.
|
| 37 |
-
.chat-header
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
"""
|
| 39 |
|
| 40 |
-
# Logo
|
| 41 |
-
logo_path = "logo.png" #
|
| 42 |
|
| 43 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as app:
|
| 44 |
-
# Header
|
| 45 |
with gr.Row(elem_classes="chat-header"):
|
| 46 |
gr.Image(value=logo_path, show_label=False)
|
| 47 |
-
gr.Markdown("## ZeppFusion")
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
chatbot = gr.Chatbot()
|
| 50 |
-
msg = gr.Textbox(placeholder="Таны асуулт...")
|
| 51 |
clear = gr.Button("🗑️ Clear History")
|
| 52 |
|
| 53 |
# Submit chat
|
| 54 |
-
msg.submit(chat, [msg, chatbot], [chatbot, chatbot])
|
| 55 |
|
| 56 |
# Clear chat history
|
| 57 |
clear.click(lambda: [], None, chatbot)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
if __name__ == "__main__":
|
| 60 |
app.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
+
import time
|
| 5 |
+
from markdown import markdown
|
| 6 |
|
| 7 |
# 🔑 Gemini API key
|
| 8 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
| 9 |
if gemini_api_key is None:
|
| 10 |
raise ValueError("⚠️ GEMINI_API_KEY тохируулаагүй байна!")
|
| 11 |
|
| 12 |
+
# Gemini API call
|
| 13 |
def gemini_response(prompt):
|
| 14 |
try:
|
| 15 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={gemini_api_key}"
|
| 16 |
headers = {"Content-Type": "application/json"}
|
| 17 |
+
data = {"contents":[{"parts":[{"text":prompt}]}]}
|
| 18 |
res = requests.post(url, headers=headers, json=data)
|
| 19 |
res_json = res.json()
|
| 20 |
return res_json["candidates"][0]["content"]["parts"][0]["text"]
|
| 21 |
except Exception as e:
|
| 22 |
return f"⚠️ Gemini error: {e}"
|
| 23 |
|
| 24 |
+
# Chat function with typing effect + markdown rendering
|
| 25 |
+
def chat(prompt, history=[], theme="Dark"):
|
| 26 |
+
history.append((f"🧑 {prompt}", None))
|
| 27 |
+
time.sleep(0.5) # simulate typing
|
| 28 |
+
reply_raw = gemini_response(prompt)
|
| 29 |
+
reply_md = markdown(f"🤖 {reply_raw}")
|
| 30 |
+
history[-1] = (history[-1][0], reply_md)
|
| 31 |
return history, history
|
| 32 |
|
| 33 |
+
# OAuth placeholder (Google login)
|
| 34 |
+
def oauth_login():
|
| 35 |
+
return "✅ Logged in (Google OAuth placeholder)"
|
| 36 |
+
|
| 37 |
+
# CSS for premium UI + theme + animations
|
| 38 |
css = """
|
| 39 |
+
.gradio-container {font-family: 'Segoe UI', sans-serif;}
|
| 40 |
+
textarea, input {border-radius:15px; padding:10px;}
|
| 41 |
+
button {border-radius:15px; padding:10px 20px; font-weight:bold;}
|
| 42 |
+
.gradio-chatbot-message.user {background-color:#0f2c54; color:#fff; border-radius:20px; padding:12px; margin:5px 0; transition: transform 0.2s;}
|
| 43 |
+
.gradio-chatbot-message.user:hover {transform: scale(1.02);}
|
| 44 |
+
.gradio-chatbot-message.bot {background-color:#1f1f1f; color:#00d9ff; border-radius:20px; padding:12px; margin:5px 0; transition: transform 0.2s;}
|
| 45 |
+
.gradio-chatbot-message.bot:hover {transform: scale(1.02);}
|
| 46 |
+
.chat-header {display:flex; align-items:center; gap:10px; justify-content: space-between;}
|
| 47 |
+
.chat-header img {height:50px; width:50px; border-radius:12px;}
|
| 48 |
+
.menu {display:flex; gap:15px; align-items:center;}
|
| 49 |
+
.loader {border:4px solid #f3f3f3; border-top:4px solid #00d9ff; border-radius:50%; width:20px; height:20px; animation: spin 1s linear infinite;}
|
| 50 |
+
@keyframes spin {100% {transform: rotate(360deg);}}
|
| 51 |
+
.scrollable-card {max-height:200px; overflow-y:auto; border:1px solid #444; border-radius:10px; padding:10px; margin:5px 0; background-color:#1a1a1a; color:#00d9ff;}
|
| 52 |
"""
|
| 53 |
|
| 54 |
+
# Logo path
|
| 55 |
+
logo_path = "logo.png" # upload to Space
|
| 56 |
|
| 57 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as app:
|
| 58 |
+
# Header
|
| 59 |
with gr.Row(elem_classes="chat-header"):
|
| 60 |
gr.Image(value=logo_path, show_label=False)
|
| 61 |
+
gr.Markdown("## ZeppFusion AI")
|
| 62 |
+
with gr.Row(elem_classes="menu"):
|
| 63 |
+
theme_btn = gr.Dropdown(["Dark","Light"], value="Dark", label="Theme")
|
| 64 |
+
login_btn = gr.Button("Login with Google")
|
| 65 |
+
|
| 66 |
chatbot = gr.Chatbot()
|
| 67 |
+
msg = gr.Textbox(placeholder="Таны асуулт...", lines=1)
|
| 68 |
clear = gr.Button("🗑️ Clear History")
|
| 69 |
|
| 70 |
# Submit chat
|
| 71 |
+
msg.submit(chat, [msg, chatbot, theme_btn], [chatbot, chatbot])
|
| 72 |
|
| 73 |
# Clear chat history
|
| 74 |
clear.click(lambda: [], None, chatbot)
|
| 75 |
|
| 76 |
+
# OAuth login
|
| 77 |
+
login_btn.click(oauth_login, None, None)
|
| 78 |
+
|
| 79 |
if __name__ == "__main__":
|
| 80 |
app.launch()
|