Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,51 @@ import os
|
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
import time
|
|
|
|
|
|
|
| 5 |
from markdown import markdown
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}"
|
|
@@ -21,60 +58,99 @@ def gemini_response(prompt):
|
|
| 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)
|
| 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 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 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 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as app:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
app.launch()
|
|
|
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
import time
|
| 5 |
+
import json
|
| 6 |
+
import hashlib
|
| 7 |
from markdown import markdown
|
| 8 |
|
| 9 |
+
# ======================
|
| 10 |
+
# 🔐 USER AUTH SYSTEM
|
| 11 |
+
# ======================
|
| 12 |
+
|
| 13 |
+
DB_PATH = "users.json"
|
| 14 |
+
|
| 15 |
+
def hash_pw(pw):
|
| 16 |
+
return hashlib.sha256(pw.encode()).hexdigest()
|
| 17 |
+
|
| 18 |
+
def load_users():
|
| 19 |
+
if not os.path.exists(DB_PATH):
|
| 20 |
+
return {}
|
| 21 |
+
return json.load(open(DB_PATH, "r"))
|
| 22 |
+
|
| 23 |
+
def save_users(data):
|
| 24 |
+
json.dump(data, open(DB_PATH, "w"), indent=4)
|
| 25 |
+
|
| 26 |
+
def signup(username, password):
|
| 27 |
+
users = load_users()
|
| 28 |
+
if username in users:
|
| 29 |
+
return "❌ Хэрэглэгч байна!"
|
| 30 |
+
users[username] = hash_pw(password)
|
| 31 |
+
save_users(users)
|
| 32 |
+
return "✅ Амжилттай бүртгэгдлээ!"
|
| 33 |
+
|
| 34 |
+
def login(username, password):
|
| 35 |
+
users = load_users()
|
| 36 |
+
if username not in users:
|
| 37 |
+
return "❌ Хэрэглэгч олдсонгүй!"
|
| 38 |
+
if users[username] != hash_pw(password):
|
| 39 |
+
return "❌ Нууц үг буруу!"
|
| 40 |
+
return "SUCCESS"
|
| 41 |
+
|
| 42 |
+
# ======================
|
| 43 |
+
# 🔑 Gemini API
|
| 44 |
+
# ======================
|
| 45 |
+
|
| 46 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
| 47 |
if gemini_api_key is None:
|
| 48 |
raise ValueError("⚠️ GEMINI_API_KEY тохируулаагүй байна!")
|
| 49 |
|
|
|
|
| 50 |
def gemini_response(prompt):
|
| 51 |
try:
|
| 52 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={gemini_api_key}"
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
return f"⚠️ Gemini error: {e}"
|
| 60 |
|
|
|
|
| 61 |
def chat(prompt, history=[], theme="Dark"):
|
| 62 |
history.append((f"🧑 {prompt}", None))
|
| 63 |
+
time.sleep(0.5)
|
| 64 |
reply_raw = gemini_response(prompt)
|
| 65 |
reply_md = markdown(f"🤖 {reply_raw}")
|
| 66 |
history[-1] = (history[-1][0], reply_md)
|
| 67 |
return history, history
|
| 68 |
|
| 69 |
+
# ======================
|
| 70 |
+
# 🌙 UI CSS
|
| 71 |
+
# ======================
|
| 72 |
|
|
|
|
| 73 |
css = """
|
| 74 |
.gradio-container {font-family: 'Segoe UI', sans-serif;}
|
| 75 |
textarea, input {border-radius:15px; padding:10px;}
|
| 76 |
button {border-radius:15px; padding:10px 20px; font-weight:bold;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
"""
|
| 78 |
|
| 79 |
+
logo_path = "logo.png"
|
| 80 |
+
|
| 81 |
+
# ======================
|
| 82 |
+
# 🚀 FULL APP UI
|
| 83 |
+
# ======================
|
| 84 |
|
| 85 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as app:
|
| 86 |
+
|
| 87 |
+
gr.Markdown("## 🔐 Welcome to ZeppFusion Login System")
|
| 88 |
+
|
| 89 |
+
with gr.Tabs():
|
| 90 |
+
|
| 91 |
+
# -------------------
|
| 92 |
+
# LOGIN TAB
|
| 93 |
+
# -------------------
|
| 94 |
+
with gr.Tab("Login"):
|
| 95 |
+
log_user = gr.Textbox(label="Username")
|
| 96 |
+
log_pw = gr.Textbox(label="Password", type="password")
|
| 97 |
+
log_status = gr.Textbox(label="Status", interactive=False)
|
| 98 |
+
log_btn = gr.Button("Login")
|
| 99 |
+
|
| 100 |
+
# -------------------
|
| 101 |
+
# SIGNUP TAB
|
| 102 |
+
# -------------------
|
| 103 |
+
with gr.Tab("Signup"):
|
| 104 |
+
reg_user = gr.Textbox(label="New Username")
|
| 105 |
+
reg_pw = gr.Textbox(label="New Password", type="password")
|
| 106 |
+
reg_status = gr.Textbox(label="Status", interactive=False)
|
| 107 |
+
reg_btn = gr.Button("Signup")
|
| 108 |
+
|
| 109 |
+
# Hidden login flag
|
| 110 |
+
logged_in = gr.State(False)
|
| 111 |
+
|
| 112 |
+
# -------------------
|
| 113 |
+
# CHAT UI (after login)
|
| 114 |
+
# -------------------
|
| 115 |
+
with gr.Group(visible=False) as chat_ui:
|
| 116 |
+
with gr.Row():
|
| 117 |
+
gr.Image(value=logo_path, show_label=False)
|
| 118 |
+
gr.Markdown("## ZeppFusion AI")
|
| 119 |
+
|
| 120 |
+
chatbot = gr.Chatbot()
|
| 121 |
+
msg = gr.Textbox(placeholder="Таны асуулт...", lines=1)
|
| 122 |
+
clear = gr.Button("🗑️ Clear History")
|
| 123 |
+
|
| 124 |
+
msg.submit(chat, [msg, chatbot, None], [chatbot, chatbot])
|
| 125 |
+
clear.click(lambda: [], None, chatbot)
|
| 126 |
+
|
| 127 |
+
# -------------------
|
| 128 |
+
# BUTTON ACTIONS
|
| 129 |
+
# -------------------
|
| 130 |
+
|
| 131 |
+
# Signup action
|
| 132 |
+
reg_btn.click(
|
| 133 |
+
signup,
|
| 134 |
+
inputs=[reg_user, reg_pw],
|
| 135 |
+
outputs=reg_status
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# Login action
|
| 139 |
+
def login_action(username, password):
|
| 140 |
+
res = login(username, password)
|
| 141 |
+
if res == "SUCCESS":
|
| 142 |
+
return "✅ Амжилттай нэвтэрлээ!", True, gr.update(visible=True)
|
| 143 |
+
return res, False, gr.update(visible=False)
|
| 144 |
+
|
| 145 |
+
log_btn.click(
|
| 146 |
+
login_action,
|
| 147 |
+
inputs=[log_user, log_pw],
|
| 148 |
+
outputs=[log_status, logged_in, chat_ui]
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
# -----------------------
|
| 152 |
+
# RUN
|
| 153 |
+
# -----------------------
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
| 156 |
app.launch()
|