Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
-
#
|
| 2 |
-
#
|
| 3 |
-
#
|
|
|
|
| 4 |
|
| 5 |
import time
|
| 6 |
import requests
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
# ---------------------------
|
| 10 |
-
# HARDCODED KEYS (TESTING)
|
| 11 |
# ---------------------------
|
| 12 |
GEMINI_KEY = "AIzaSyAPfDiu2V_aD6un00qHt5bkISm6C0Pkx7o"
|
| 13 |
GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent"
|
|
@@ -16,6 +17,7 @@ GROQ_KEY = "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"
|
|
| 16 |
GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 17 |
GROQ_MODEL = "meta-llama/llama-4-scout-17b-16e-instruct"
|
| 18 |
|
|
|
|
| 19 |
# ---------------------------
|
| 20 |
# Helpers
|
| 21 |
# ---------------------------
|
|
@@ -31,35 +33,42 @@ def post_with_retries(url, headers, payload, timeout=18, max_retries=2):
|
|
| 31 |
time.sleep(0.5 + i)
|
| 32 |
raise Exception("Max retries exceeded")
|
| 33 |
|
|
|
|
| 34 |
def call_gemini(api_key, message, history):
|
| 35 |
-
headers = {"Content-Type":"application/json", "x-goog-api-key": api_key}
|
|
|
|
| 36 |
contents = []
|
| 37 |
for u, m in history:
|
| 38 |
-
contents.append({"role":"user","parts":[{"text":u}]})
|
| 39 |
-
contents.append({"role":"model","parts":[{"text":m}]})
|
| 40 |
-
contents.append({"role":"user","parts":[{"text":message}]})
|
| 41 |
payload = {"contents": contents}
|
|
|
|
| 42 |
r = post_with_retries(GEMINI_URL, headers, payload)
|
| 43 |
data = r.json()
|
| 44 |
-
return data.get("candidates",[{}])[0].get("content",{}).get("parts",[{}])[0].get("text","")
|
|
|
|
| 45 |
|
| 46 |
def call_llama_via_groq(api_key, model, message, history):
|
| 47 |
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
|
|
|
| 48 |
msgs = []
|
| 49 |
for u, m in history:
|
| 50 |
-
msgs.append({"role":"user","content":u})
|
| 51 |
-
msgs.append({"role":"assistant","content":m})
|
| 52 |
-
msgs.append({"role":"user","content":message})
|
| 53 |
payload = {"model": model, "messages": msgs}
|
|
|
|
| 54 |
r = post_with_retries(GROQ_URL, headers, payload)
|
| 55 |
data = r.json()
|
| 56 |
if "choices" in data and data["choices"]:
|
| 57 |
ch = data["choices"][0]
|
| 58 |
if isinstance(ch.get("message"), dict):
|
| 59 |
-
return ch["message"].get("content","")
|
| 60 |
-
return ch.get("text","")
|
| 61 |
return str(data)
|
| 62 |
|
|
|
|
| 63 |
# ---------------------------
|
| 64 |
# Chat function
|
| 65 |
# ---------------------------
|
|
@@ -73,68 +82,29 @@ def chat_fn(message, history, model_choice):
|
|
| 73 |
ans = f"Error: {e}"
|
| 74 |
return ans
|
| 75 |
|
|
|
|
| 76 |
# ---------------------------
|
| 77 |
-
#
|
| 78 |
# ---------------------------
|
| 79 |
css = """
|
| 80 |
-
/*
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
}
|
| 95 |
-
#title {
|
| 96 |
-
font-weight:800;
|
| 97 |
-
color:#ffcc33;
|
| 98 |
-
font-size:20px;
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
/* Dropdown */
|
| 102 |
-
#model_dropdown .gr-dropdown {
|
| 103 |
-
background:#ffffff !important;
|
| 104 |
-
border:1px solid #d0d0d0 !important;
|
| 105 |
-
color:#000000 !important;
|
| 106 |
-
padding:10px 12px !important;
|
| 107 |
-
border-radius:8px !important;
|
| 108 |
-
width:260px !important;
|
| 109 |
-
box-shadow:none !important;
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
/* Chat area */
|
| 113 |
-
.gradio-container .chat-interface .chatbot {
|
| 114 |
-
background:#ffffff !important;
|
| 115 |
-
color:#000000 !important;
|
| 116 |
-
min-height: calc(100vh - 220px);
|
| 117 |
-
border:1px solid #e0e0e0;
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
/* Input textbox */
|
| 121 |
-
textarea, .gr-textbox {
|
| 122 |
-
background:#ffffff !important;
|
| 123 |
-
color:#000000 !important;
|
| 124 |
-
border:1px solid #d0d0d0 !important;
|
| 125 |
-
border-radius:8px !important;
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
/* Send button */
|
| 129 |
-
.gr-button {
|
| 130 |
-
background:#ffcc33 !important;
|
| 131 |
-
color:#000000 !important;
|
| 132 |
-
border:none !important;
|
| 133 |
-
border-radius:10px !important;
|
| 134 |
-
font-weight:700;
|
| 135 |
-
}
|
| 136 |
"""
|
| 137 |
|
|
|
|
| 138 |
# ---------------------------
|
| 139 |
# Build UI
|
| 140 |
# ---------------------------
|
|
@@ -150,8 +120,8 @@ with gr.Blocks(css=css, title="⚡ YellowFlash.ai") as app:
|
|
| 150 |
|
| 151 |
gr.ChatInterface(
|
| 152 |
fn=chat_fn,
|
| 153 |
-
title="⚡
|
| 154 |
-
description="
|
| 155 |
additional_inputs=[model_dropdown],
|
| 156 |
)
|
| 157 |
|
|
|
|
| 1 |
+
# yellowflash_darkmode_with_memory.py
|
| 2 |
+
# Uses native gr.ChatInterface with a top-left model dropdown and top-right title.
|
| 3 |
+
# Adds session-only chat memory (from history).
|
| 4 |
+
# TESTING ONLY: hardcoded keys included. Do not publish.
|
| 5 |
|
| 6 |
import time
|
| 7 |
import requests
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
# ---------------------------
|
| 11 |
+
# HARDCODED KEYS (TESTING ONLY)
|
| 12 |
# ---------------------------
|
| 13 |
GEMINI_KEY = "AIzaSyAPfDiu2V_aD6un00qHt5bkISm6C0Pkx7o"
|
| 14 |
GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent"
|
|
|
|
| 17 |
GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 18 |
GROQ_MODEL = "meta-llama/llama-4-scout-17b-16e-instruct"
|
| 19 |
|
| 20 |
+
|
| 21 |
# ---------------------------
|
| 22 |
# Helpers
|
| 23 |
# ---------------------------
|
|
|
|
| 33 |
time.sleep(0.5 + i)
|
| 34 |
raise Exception("Max retries exceeded")
|
| 35 |
|
| 36 |
+
|
| 37 |
def call_gemini(api_key, message, history):
|
| 38 |
+
headers = {"Content-Type": "application/json", "x-goog-api-key": api_key}
|
| 39 |
+
# include chat history for memory
|
| 40 |
contents = []
|
| 41 |
for u, m in history:
|
| 42 |
+
contents.append({"role": "user", "parts": [{"text": u}]})
|
| 43 |
+
contents.append({"role": "model", "parts": [{"text": m}]})
|
| 44 |
+
contents.append({"role": "user", "parts": [{"text": message}]})
|
| 45 |
payload = {"contents": contents}
|
| 46 |
+
|
| 47 |
r = post_with_retries(GEMINI_URL, headers, payload)
|
| 48 |
data = r.json()
|
| 49 |
+
return data.get("candidates", [{}])[0].get("content", {}).get("parts", [{}])[0].get("text", "")
|
| 50 |
+
|
| 51 |
|
| 52 |
def call_llama_via_groq(api_key, model, message, history):
|
| 53 |
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
| 54 |
+
# include chat history for memory
|
| 55 |
msgs = []
|
| 56 |
for u, m in history:
|
| 57 |
+
msgs.append({"role": "user", "content": u})
|
| 58 |
+
msgs.append({"role": "assistant", "content": m})
|
| 59 |
+
msgs.append({"role": "user", "content": message})
|
| 60 |
payload = {"model": model, "messages": msgs}
|
| 61 |
+
|
| 62 |
r = post_with_retries(GROQ_URL, headers, payload)
|
| 63 |
data = r.json()
|
| 64 |
if "choices" in data and data["choices"]:
|
| 65 |
ch = data["choices"][0]
|
| 66 |
if isinstance(ch.get("message"), dict):
|
| 67 |
+
return ch["message"].get("content", "")
|
| 68 |
+
return ch.get("text", "")
|
| 69 |
return str(data)
|
| 70 |
|
| 71 |
+
|
| 72 |
# ---------------------------
|
| 73 |
# Chat function
|
| 74 |
# ---------------------------
|
|
|
|
| 82 |
ans = f"Error: {e}"
|
| 83 |
return ans
|
| 84 |
|
| 85 |
+
|
| 86 |
# ---------------------------
|
| 87 |
+
# Dark Mode CSS
|
| 88 |
# ---------------------------
|
| 89 |
css = """
|
| 90 |
+
/* topbar layout */
|
| 91 |
+
#topbar { display:flex; justify-content:space-between; align-items:center;
|
| 92 |
+
padding:18px 28px; background:#0f0f0f; border-bottom:1px solid #1f1f1f; }
|
| 93 |
+
#title { font-weight:800; color:#ffcc33; font-size:20px; }
|
| 94 |
+
|
| 95 |
+
/* compact, flat dropdown look */
|
| 96 |
+
#model_dropdown .gr-dropdown { background:#1a1a1a !important; border:1px solid #2b2b2b !important;
|
| 97 |
+
color:#ddd !important; padding:10px 12px !important; border-radius:8px !important;
|
| 98 |
+
width:260px !important; box-shadow:none !important; }
|
| 99 |
+
|
| 100 |
+
/* make ChatInterface chat area taller */
|
| 101 |
+
.gradio-container .chat-interface .chatbot { min-height: calc(100vh - 220px); background:#111; color:#eee; }
|
| 102 |
+
|
| 103 |
+
/* style send button */
|
| 104 |
+
.gr-button { border-radius:10px !important; background:#2c2c3f !important; color:#fff !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
"""
|
| 106 |
|
| 107 |
+
|
| 108 |
# ---------------------------
|
| 109 |
# Build UI
|
| 110 |
# ---------------------------
|
|
|
|
| 120 |
|
| 121 |
gr.ChatInterface(
|
| 122 |
fn=chat_fn,
|
| 123 |
+
title="⚡",
|
| 124 |
+
description="FAST AS LIGHTNING",
|
| 125 |
additional_inputs=[model_dropdown],
|
| 126 |
)
|
| 127 |
|