Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,118 +1,111 @@
|
|
| 1 |
-
#
|
| 2 |
-
#
|
| 3 |
-
# TEST ONLY: Hardcoded API keys (do not push public)
|
| 4 |
|
| 5 |
-
import time
|
| 6 |
-
import requests
|
| 7 |
import gradio as gr
|
| 8 |
-
from
|
| 9 |
|
| 10 |
# ---------------------------
|
| 11 |
# HARDCODED KEYS (TESTING)
|
| 12 |
# ---------------------------
|
| 13 |
GEMINI_KEY = "AIzaSyAPfDiu2V_aD6un00qHt5bkISm6C0Pkx7o"
|
| 14 |
-
|
| 15 |
|
| 16 |
GROQ_KEY = "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"
|
| 17 |
GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 18 |
GROQ_MODEL = "meta-llama/llama-4-scout-17b-16e-instruct"
|
| 19 |
|
| 20 |
-
# Gemini Image Gen (Imagen 2.0)
|
| 21 |
-
GENAI_CLIENT = genai.Client(api_key=GEMINI_KEY)
|
| 22 |
-
GEMINI_IMAGE_MODEL = "models/imagen-4.0-generate-001"
|
| 23 |
-
|
| 24 |
# ---------------------------
|
| 25 |
-
#
|
| 26 |
# ---------------------------
|
| 27 |
-
def
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
contents = []
|
| 42 |
for u, m in history:
|
| 43 |
contents.append({"role":"user","parts":[{"text":u}]})
|
| 44 |
contents.append({"role":"model","parts":[{"text":m}]})
|
| 45 |
contents.append({"role":"user","parts":[{"text":message}]})
|
| 46 |
payload = {"contents": contents}
|
| 47 |
-
r =
|
| 48 |
data = r.json()
|
| 49 |
return data.get("candidates",[{}])[0].get("content",{}).get("parts",[{}])[0].get("text","")
|
| 50 |
|
| 51 |
-
def
|
| 52 |
-
headers = {"Authorization": f"Bearer {
|
| 53 |
msgs = []
|
| 54 |
for u, m in history:
|
| 55 |
msgs.append({"role":"user","content":u})
|
| 56 |
msgs.append({"role":"assistant","content":m})
|
| 57 |
msgs.append({"role":"user","content":message})
|
| 58 |
-
payload = {"model":
|
| 59 |
-
r =
|
| 60 |
data = r.json()
|
| 61 |
if "choices" in data and data["choices"]:
|
| 62 |
ch = data["choices"][0]
|
| 63 |
-
|
| 64 |
-
return ch["message"].get("content","")
|
| 65 |
-
return ch.get("text","")
|
| 66 |
return str(data)
|
| 67 |
|
| 68 |
-
def call_gemini_image(prompt):
|
| 69 |
-
try:
|
| 70 |
-
result = GENAI_CLIENT.models.generate_images(
|
| 71 |
-
model=GEMINI_IMAGE_MODEL,
|
| 72 |
-
prompt=prompt,
|
| 73 |
-
config=dict(
|
| 74 |
-
number_of_images=1,
|
| 75 |
-
output_mime_type="image/jpeg",
|
| 76 |
-
person_generation="ALLOW_ALL",
|
| 77 |
-
aspect_ratio="1:1",
|
| 78 |
-
image_size="1K",
|
| 79 |
-
),
|
| 80 |
-
)
|
| 81 |
-
if not result.generated_images:
|
| 82 |
-
return None
|
| 83 |
-
img = result.generated_images[0].image
|
| 84 |
-
return img
|
| 85 |
-
except Exception as e:
|
| 86 |
-
return str(e)
|
| 87 |
-
|
| 88 |
# ---------------------------
|
| 89 |
-
#
|
| 90 |
# ---------------------------
|
| 91 |
def chat_fn(message, history, model_choice):
|
| 92 |
try:
|
| 93 |
if model_choice == "Google Gemini 2.0 Flash":
|
| 94 |
-
|
| 95 |
elif model_choice == "Meta LLaMA 4":
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
else:
|
| 98 |
-
|
| 99 |
except Exception as e:
|
| 100 |
-
|
| 101 |
-
return ans
|
| 102 |
|
| 103 |
# ---------------------------
|
| 104 |
# CSS
|
| 105 |
# ---------------------------
|
| 106 |
css = """
|
| 107 |
-
#topbar { display:flex; justify-content:space-between; align-items:center;
|
|
|
|
| 108 |
#title { font-weight:800; color:#ffcc33; font-size:20px; }
|
| 109 |
-
#model_dropdown .gr-dropdown { background:transparent !important;
|
|
|
|
|
|
|
| 110 |
.gradio-container .chat-interface .chatbot { min-height: calc(100vh - 220px); }
|
| 111 |
.gr-button { border-radius:10px !important; }
|
| 112 |
"""
|
| 113 |
|
| 114 |
# ---------------------------
|
| 115 |
-
#
|
| 116 |
# ---------------------------
|
| 117 |
with gr.Blocks(css=css, title="⚡ YellowFlash.ai") as app:
|
| 118 |
with gr.Row(elem_id="topbar"):
|
|
@@ -122,22 +115,13 @@ with gr.Blocks(css=css, title="⚡ YellowFlash.ai") as app:
|
|
| 122 |
show_label=False,
|
| 123 |
elem_id="model_dropdown"
|
| 124 |
)
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
chat = gr.ChatInterface(
|
| 128 |
fn=chat_fn,
|
| 129 |
title="⚡",
|
| 130 |
-
description="FAST AS LIGHTNING",
|
| 131 |
additional_inputs=[model_dropdown],
|
| 132 |
)
|
| 133 |
|
| 134 |
-
# Image Gen UI
|
| 135 |
-
with gr.Group():
|
| 136 |
-
gr.Markdown("### 🖼️ Gemini Imagen 2.0 - Generate Images")
|
| 137 |
-
with gr.Row():
|
| 138 |
-
img_prompt = gr.Textbox(label="Enter your prompt", scale=4)
|
| 139 |
-
gen_btn = gr.Button("Generate Image", scale=1)
|
| 140 |
-
img_output = gr.Image(label="Generated Image", type="pil")
|
| 141 |
-
gen_btn.click(fn=call_gemini_image, inputs=img_prompt, outputs=img_output)
|
| 142 |
-
|
| 143 |
app.launch(share=True)
|
|
|
|
| 1 |
+
# yellowflash_chat_with_image.py
|
| 2 |
+
# Native ChatInterface + Gemini image gen in-chat
|
|
|
|
| 3 |
|
| 4 |
+
import time, requests, io, base64
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
+
from PIL import Image
|
| 7 |
|
| 8 |
# ---------------------------
|
| 9 |
# HARDCODED KEYS (TESTING)
|
| 10 |
# ---------------------------
|
| 11 |
GEMINI_KEY = "AIzaSyAPfDiu2V_aD6un00qHt5bkISm6C0Pkx7o"
|
| 12 |
+
GEMINI_TEXT_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent"
|
| 13 |
|
| 14 |
GROQ_KEY = "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"
|
| 15 |
GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 16 |
GROQ_MODEL = "meta-llama/llama-4-scout-17b-16e-instruct"
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# ---------------------------
|
| 19 |
+
# IMAGE GENERATION CALL (using google-genai style API)
|
| 20 |
# ---------------------------
|
| 21 |
+
def call_gemini_image(prompt_text):
|
| 22 |
+
try:
|
| 23 |
+
# NOTE: requires google-genai installed and correct endpoint
|
| 24 |
+
from google import genai
|
| 25 |
+
client = genai.Client(api_key=GEMINI_KEY)
|
| 26 |
+
result = client.models.generate_images(
|
| 27 |
+
model="models/imagen-4.0-generate-001",
|
| 28 |
+
prompt=prompt_text,
|
| 29 |
+
config=dict(number_of_images=1, output_mime_type="image/jpeg"),
|
| 30 |
+
)
|
| 31 |
+
if not result.generated_images:
|
| 32 |
+
return "⚠️ No image generated."
|
| 33 |
|
| 34 |
+
gi = result.generated_images[0]
|
| 35 |
+
if hasattr(gi, "image"):
|
| 36 |
+
return gi.image # PIL.Image -> Gradio can render inline
|
| 37 |
+
if hasattr(gi, "dataURI"):
|
| 38 |
+
header, b64 = gi.dataURI.split(",", 1)
|
| 39 |
+
img_bytes = io.BytesIO(base64.b64decode(b64))
|
| 40 |
+
return Image.open(img_bytes).convert("RGB")
|
| 41 |
+
return "⚠️ Unknown image response."
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Image generation error: {e}"
|
| 44 |
+
|
| 45 |
+
# ---------------------------
|
| 46 |
+
# TEXT CALLS
|
| 47 |
+
# ---------------------------
|
| 48 |
+
def call_gemini_text(message, history):
|
| 49 |
+
headers = {"Content-Type":"application/json","x-goog-api-key": GEMINI_KEY}
|
| 50 |
contents = []
|
| 51 |
for u, m in history:
|
| 52 |
contents.append({"role":"user","parts":[{"text":u}]})
|
| 53 |
contents.append({"role":"model","parts":[{"text":m}]})
|
| 54 |
contents.append({"role":"user","parts":[{"text":message}]})
|
| 55 |
payload = {"contents": contents}
|
| 56 |
+
r = requests.post(GEMINI_TEXT_URL, headers=headers, json=payload, timeout=20)
|
| 57 |
data = r.json()
|
| 58 |
return data.get("candidates",[{}])[0].get("content",{}).get("parts",[{}])[0].get("text","")
|
| 59 |
|
| 60 |
+
def call_llama_text(message, history):
|
| 61 |
+
headers = {"Authorization": f"Bearer {GROQ_KEY}", "Content-Type":"application/json"}
|
| 62 |
msgs = []
|
| 63 |
for u, m in history:
|
| 64 |
msgs.append({"role":"user","content":u})
|
| 65 |
msgs.append({"role":"assistant","content":m})
|
| 66 |
msgs.append({"role":"user","content":message})
|
| 67 |
+
payload = {"model": GROQ_MODEL, "messages": msgs}
|
| 68 |
+
r = requests.post(GROQ_URL, headers=headers, json=payload, timeout=20)
|
| 69 |
data = r.json()
|
| 70 |
if "choices" in data and data["choices"]:
|
| 71 |
ch = data["choices"][0]
|
| 72 |
+
return ch.get("message",{}).get("content","")
|
|
|
|
|
|
|
| 73 |
return str(data)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
# ---------------------------
|
| 76 |
+
# CHAT FUNCTION
|
| 77 |
# ---------------------------
|
| 78 |
def chat_fn(message, history, model_choice):
|
| 79 |
try:
|
| 80 |
if model_choice == "Google Gemini 2.0 Flash":
|
| 81 |
+
return call_gemini_text(message, history)
|
| 82 |
elif model_choice == "Meta LLaMA 4":
|
| 83 |
+
return call_llama_text(message, history)
|
| 84 |
+
elif model_choice == "Gemini Imagen 2.0":
|
| 85 |
+
# Return image directly in chat
|
| 86 |
+
img = call_gemini_image(message)
|
| 87 |
+
return img
|
| 88 |
else:
|
| 89 |
+
return "⚠️ Unknown model selected."
|
| 90 |
except Exception as e:
|
| 91 |
+
return f"Error: {e}"
|
|
|
|
| 92 |
|
| 93 |
# ---------------------------
|
| 94 |
# CSS
|
| 95 |
# ---------------------------
|
| 96 |
css = """
|
| 97 |
+
#topbar { display:flex; justify-content:space-between; align-items:center;
|
| 98 |
+
padding:16px 22px; background:#0f0f0f; border-bottom:1px solid #1f1f1f; }
|
| 99 |
#title { font-weight:800; color:#ffcc33; font-size:20px; }
|
| 100 |
+
#model_dropdown .gr-dropdown { background:transparent !important;
|
| 101 |
+
border:1px solid #2b2b2b !important; color:#ddd !important;
|
| 102 |
+
padding:8px 12px !important; border-radius:8px !important; width:260px !important; }
|
| 103 |
.gradio-container .chat-interface .chatbot { min-height: calc(100vh - 220px); }
|
| 104 |
.gr-button { border-radius:10px !important; }
|
| 105 |
"""
|
| 106 |
|
| 107 |
# ---------------------------
|
| 108 |
+
# BUILD UI
|
| 109 |
# ---------------------------
|
| 110 |
with gr.Blocks(css=css, title="⚡ YellowFlash.ai") as app:
|
| 111 |
with gr.Row(elem_id="topbar"):
|
|
|
|
| 115 |
show_label=False,
|
| 116 |
elem_id="model_dropdown"
|
| 117 |
)
|
| 118 |
+
gr.HTML("<div id='title'>⚡ YellowFlash.ai</div>")
|
| 119 |
|
| 120 |
+
gr.ChatInterface(
|
|
|
|
| 121 |
fn=chat_fn,
|
| 122 |
title="⚡",
|
| 123 |
+
description="FAST AS LIGHTNING — text + image in one UI",
|
| 124 |
additional_inputs=[model_dropdown],
|
| 125 |
)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
app.launch(share=True)
|