Spaces:
Sleeping
Sleeping
Update app.py
#1
by
manvithll - opened
app.py
CHANGED
|
@@ -1,95 +1,171 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
| 12 |
MODELS = {
|
| 13 |
-
"Gemini
|
|
|
|
| 14 |
"api_url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent",
|
| 15 |
"api_key": GOOGLE_API_KEY,
|
| 16 |
"handler": "gemini"
|
| 17 |
},
|
| 18 |
-
"
|
|
|
|
| 19 |
"api_url": "https://api.groq.com/openai/v1/chat/completions",
|
| 20 |
-
"api_key":
|
| 21 |
-
|
| 22 |
-
"
|
|
|
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def chat_fn(message, history, selected_model):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
if "
|
| 34 |
-
return f"Error: API key for {selected_model}
|
|
|
|
| 35 |
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
api_history.append({"role": "user", "parts": [{"text": message}]})
|
| 47 |
-
|
| 48 |
-
json_payload = {"contents": api_history}
|
| 49 |
-
|
| 50 |
-
resp = requests.post(api_url, headers=headers, json=json_payload, timeout=30)
|
| 51 |
-
resp.raise_for_status()
|
| 52 |
-
ans = resp.json()["candidates"][0]["content"]["parts"][0]["text"]
|
| 53 |
-
|
| 54 |
-
# --- OpenAI API Logic ---
|
| 55 |
-
elif handler_type == "openai":
|
| 56 |
-
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
| 57 |
-
|
| 58 |
-
# FIXED: Changed the broken list comprehension to a standard for loop
|
| 59 |
-
api_history = []
|
| 60 |
-
for user_msg, model_msg in history:
|
| 61 |
-
api_history.append({"role": "user", "content": user_msg})
|
| 62 |
-
api_history.append({"role": "assistant", "content": model_msg})
|
| 63 |
-
api_history.append({"role": "user", "content": message})
|
| 64 |
-
|
| 65 |
-
json_payload = {"messages": api_history, "model": config["model_name"]}
|
| 66 |
-
|
| 67 |
-
resp = requests.post(api_url, headers=headers, json=json_payload, timeout=30)
|
| 68 |
-
resp.raise_for_status()
|
| 69 |
-
ans = resp.json()["choices"][0]["message"]["content"]
|
| 70 |
|
| 71 |
-
except requests.exceptions.RequestException as e:
|
| 72 |
-
ans = f"Network Error: {e}"
|
| 73 |
except Exception as e:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
#
|
|
|
|
|
|
|
| 79 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 80 |
-
gr.Markdown("# ⚡Yellowflash
|
| 81 |
-
gr.Markdown("Select a model and start
|
| 82 |
-
|
| 83 |
with gr.Row():
|
| 84 |
-
model_selector = gr.Radio(
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
demo.launch(share=True)
|
|
|
|
| 1 |
+
# yellowflash_multi_backend.py
|
| 2 |
+
# Minimal Gradio chat interface with Gemini (Google) and Groq (OpenAI-compatible)
|
| 3 |
+
# WARNING: This file contains placeholders for API keys. Replace with your keys locally ONLY.
|
| 4 |
|
| 5 |
+
import os
|
| 6 |
+
import time
|
| 7 |
+
import random
|
| 8 |
+
import requests
|
| 9 |
+
import gradio as gr
|
| 10 |
|
| 11 |
+
# -------------------------
|
| 12 |
+
# 1) HARDCODED KEYS (replace with your real keys)
|
| 13 |
+
# -------------------------
|
| 14 |
+
# Replace the placeholders below with your actual keys if you insist on hardcoding.
|
| 15 |
+
# DO NOT commit or share the file with real keys.
|
| 16 |
+
GOOGLE_API_KEY = "AIzaSyAPfDiu2V_aD6un00qHt5bkISm6C0Pkx7o" # e.g. from makersuite/google Cloud
|
| 17 |
+
GROQ_API_KEY = "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" # starts with gsk_... from Groq Console
|
| 18 |
|
| 19 |
+
# -------------------------
|
| 20 |
+
# 2) MODELS config (Gemini + Groq)
|
| 21 |
+
# -------------------------
|
| 22 |
MODELS = {
|
| 23 |
+
"Gemini 1.5 Pro": {
|
| 24 |
+
# Gemini content generation endpoint (example from earlier)
|
| 25 |
"api_url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent",
|
| 26 |
"api_key": GOOGLE_API_KEY,
|
| 27 |
"handler": "gemini"
|
| 28 |
},
|
| 29 |
+
"Groq LLaMA-4": {
|
| 30 |
+
# Groq's OpenAI-compatible chat completions URL (from console screenshot)
|
| 31 |
"api_url": "https://api.groq.com/openai/v1/chat/completions",
|
| 32 |
+
"api_key": GROQ_API_KEY,
|
| 33 |
+
# Example model name shown in Groq UI—change if different
|
| 34 |
+
"model_name": "meta-llama/llama-4-scout-17b-16e-instruct",
|
| 35 |
+
"handler": "openai_compat"
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
+
# -------------------------
|
| 40 |
+
# 3) Retry helper with exponential backoff + jitter
|
| 41 |
+
# -------------------------
|
| 42 |
+
def post_with_retries(url, headers, json_payload, timeout=30,
|
| 43 |
+
max_retries=5, base_delay=1.0, max_delay=20.0):
|
| 44 |
+
attempt = 0
|
| 45 |
+
last_exc = None
|
| 46 |
+
while attempt <= max_retries:
|
| 47 |
+
try:
|
| 48 |
+
resp = requests.post(url, headers=headers, json=json_payload, timeout=timeout)
|
| 49 |
+
resp.raise_for_status()
|
| 50 |
+
return resp
|
| 51 |
+
except requests.exceptions.HTTPError as e:
|
| 52 |
+
last_exc = e
|
| 53 |
+
status = getattr(e.response, "status_code", None)
|
| 54 |
+
# Retry for rate-limit / server errors
|
| 55 |
+
if status in (429, 502, 503, 504):
|
| 56 |
+
attempt += 1
|
| 57 |
+
delay = min(max_delay, base_delay * (2 ** (attempt - 1)))
|
| 58 |
+
jitter = random.random()
|
| 59 |
+
sleep_time = delay + jitter
|
| 60 |
+
print(f"[retry] HTTP {status}, attempt {attempt}/{max_retries}, sleeping {sleep_time:.2f}s")
|
| 61 |
+
time.sleep(sleep_time)
|
| 62 |
+
continue
|
| 63 |
+
else:
|
| 64 |
+
# non-retryable HTTP error
|
| 65 |
+
raise
|
| 66 |
+
except requests.exceptions.RequestException as e:
|
| 67 |
+
last_exc = e
|
| 68 |
+
attempt += 1
|
| 69 |
+
delay = min(max_delay, base_delay * (2 ** (attempt - 1)))
|
| 70 |
+
jitter = random.random()
|
| 71 |
+
sleep_time = delay + jitter
|
| 72 |
+
print(f"[retry] Network err attempt {attempt}/{max_retries}, sleeping {sleep_time:.2f}s")
|
| 73 |
+
time.sleep(sleep_time)
|
| 74 |
+
continue
|
| 75 |
+
# exhausted retries
|
| 76 |
+
raise last_exc
|
| 77 |
+
|
| 78 |
+
# -------------------------
|
| 79 |
+
# 4) Chat logic for handlers
|
| 80 |
+
# -------------------------
|
| 81 |
+
def call_gemini(api_url, api_key, history, message):
|
| 82 |
+
headers = {"Content-Type": "application/json", "x-goog-api-key": api_key}
|
| 83 |
+
api_history = []
|
| 84 |
+
# Gemini expects 'contents' with parts per role as in earlier example
|
| 85 |
+
for user_msg, model_msg in history:
|
| 86 |
+
api_history.append({"role": "user", "parts": [{"text": user_msg}]})
|
| 87 |
+
api_history.append({"role": "model", "parts": [{"text": model_msg}]})
|
| 88 |
+
api_history.append({"role": "user", "parts": [{"text": message}]})
|
| 89 |
+
json_payload = {"contents": api_history}
|
| 90 |
+
|
| 91 |
+
resp = post_with_retries(api_url, headers, json_payload, timeout=30)
|
| 92 |
+
data = resp.json()
|
| 93 |
+
# defensive parsing
|
| 94 |
+
try:
|
| 95 |
+
ans = data.get("candidates", [{}])[0].get("content", {}).get("parts", [{}])[0].get("text", "")
|
| 96 |
+
except Exception:
|
| 97 |
+
ans = str(data)
|
| 98 |
+
return ans or "Gemini: no reply."
|
| 99 |
+
|
| 100 |
+
def call_openai_compat(api_url, api_key, model_name, history, message):
|
| 101 |
+
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
| 102 |
+
api_history = []
|
| 103 |
+
for user_msg, model_msg in history:
|
| 104 |
+
api_history.append({"role": "user", "content": user_msg})
|
| 105 |
+
api_history.append({"role": "assistant", "content": model_msg})
|
| 106 |
+
api_history.append({"role": "user", "content": message})
|
| 107 |
+
|
| 108 |
+
json_payload = {"messages": api_history, "model": model_name}
|
| 109 |
+
resp = post_with_retries(api_url, headers, json_payload, timeout=30)
|
| 110 |
+
data = resp.json()
|
| 111 |
+
# parse OpenAI-compatible response
|
| 112 |
+
if "choices" in data and data["choices"]:
|
| 113 |
+
choice = data["choices"][0]
|
| 114 |
+
if "message" in choice:
|
| 115 |
+
return choice["message"].get("content", "")
|
| 116 |
+
elif "text" in choice:
|
| 117 |
+
return choice["text"]
|
| 118 |
+
return str(data)
|
| 119 |
+
|
| 120 |
+
# -------------------------
|
| 121 |
+
# 5) Unified chat function used by Gradio
|
| 122 |
+
# -------------------------
|
| 123 |
def chat_fn(message, history, selected_model):
|
| 124 |
+
cfg = MODELS.get(selected_model)
|
| 125 |
+
if not cfg:
|
| 126 |
+
return f"Unknown model: {selected_model}"
|
| 127 |
+
|
| 128 |
+
handler = cfg["handler"]
|
| 129 |
+
api_url = cfg["api_url"]
|
| 130 |
+
api_key = cfg["api_key"]
|
| 131 |
|
| 132 |
+
if not api_key or "YOUR_" in api_key:
|
| 133 |
+
return (f"Error: API key for {selected_model} not set. "
|
| 134 |
+
"Replace placeholder with key string in the script or use environment vars.")
|
| 135 |
|
| 136 |
try:
|
| 137 |
+
if handler == "gemini":
|
| 138 |
+
return call_gemini(api_url, api_key, history, message)
|
| 139 |
+
|
| 140 |
+
elif handler == "openai_compat":
|
| 141 |
+
model_name = cfg.get("model_name")
|
| 142 |
+
return call_openai_compat(api_url, api_key, model_name, history, message)
|
| 143 |
+
|
| 144 |
+
else:
|
| 145 |
+
return f"Unsupported handler: {handler}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
|
|
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
+
# avoid printing full responses that might contain secrets
|
| 149 |
+
try:
|
| 150 |
+
err_text = str(e)
|
| 151 |
+
except Exception:
|
| 152 |
+
err_text = "Unknown error"
|
| 153 |
+
return f"Error: {err_text}"
|
| 154 |
|
| 155 |
+
# -------------------------
|
| 156 |
+
# 6) Gradio UI (chat)
|
| 157 |
+
# -------------------------
|
| 158 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 159 |
+
gr.Markdown("# ⚡ Yellowflash,ai")
|
| 160 |
+
gr.Markdown("Select a model and start chatting. Keys are hardcoded in the script (not secure).")
|
| 161 |
+
|
| 162 |
with gr.Row():
|
| 163 |
+
model_selector = gr.Radio(choices=list(MODELS.keys()),
|
| 164 |
+
value=list(MODELS.keys())[0],
|
| 165 |
+
label="Choose your model")
|
| 166 |
+
|
| 167 |
+
gr.ChatInterface(chat_fn, additional_inputs=[model_selector])
|
| 168 |
+
|
| 169 |
+
# throttle concurrency to reduce rate-limit issues
|
| 170 |
+
demo.queue(concurrency_count=1, max_size=20)
|
| 171 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|