Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,54 +2,42 @@ import os, io, time, requests, gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
import google.generativeai as genai
|
| 4 |
|
| 5 |
-
# Load secure keys
|
| 6 |
HF_TOKEN = os.getenv("HF_API_TOKEN")
|
| 7 |
GEMINI_KEY = os.getenv("GEMINI_API_KEY")
|
| 8 |
|
| 9 |
-
# Configure Gemini
|
| 10 |
genai.configure(api_key=GEMINI_KEY)
|
| 11 |
-
GEMINI_MODEL = "gemini-pro"
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
# Chatbot
|
| 18 |
-
def chat(query, history):
|
| 19 |
-
history = history or []
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# Image Generation
|
| 29 |
def generate_image(prompt):
|
| 30 |
-
if not prompt:
|
| 31 |
-
return None
|
| 32 |
payload = {"inputs": prompt, "options": {"wait_for_model": True}}
|
| 33 |
-
r = requests.post(HF_URL, headers=
|
| 34 |
-
|
| 35 |
-
return None
|
| 36 |
img = Image.open(io.BytesIO(r.content)).convert("RGB")
|
| 37 |
return img
|
| 38 |
|
| 39 |
-
# UI
|
| 40 |
with gr.Blocks() as app:
|
| 41 |
-
gr.Markdown("<h1 style='text-align:center
|
| 42 |
-
|
| 43 |
-
with gr.Tab("💬 Chat"):
|
| 44 |
-
chatbox = gr.Chatbot()
|
| 45 |
-
msg = gr.Textbox(label="Ask me anything")
|
| 46 |
-
send = gr.Button("Send")
|
| 47 |
-
send.click(chat, [msg, chatbox], [chatbox, chatbox])
|
| 48 |
-
|
| 49 |
-
with gr.Tab("🎨 Text to Image"):
|
| 50 |
-
prompt = gr.Textbox(label="Describe an image")
|
| 51 |
-
btn = gr.Button("Generate")
|
| 52 |
-
output = gr.Image(type="pil")
|
| 53 |
-
btn.click(generate_image, prompt, output)
|
| 54 |
-
|
| 55 |
-
app.launch()
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import google.generativeai as genai
|
| 4 |
|
|
|
|
| 5 |
HF_TOKEN = os.getenv("HF_API_TOKEN")
|
| 6 |
GEMINI_KEY = os.getenv("GEMINI_API_KEY")
|
| 7 |
|
|
|
|
| 8 |
genai.configure(api_key=GEMINI_KEY)
|
|
|
|
| 9 |
|
| 10 |
+
def chat_fn(message, history):
|
| 11 |
+
if not history:
|
| 12 |
+
history = []
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 16 |
+
response = model.generate_content(message)
|
| 17 |
+
answer = response.text
|
| 18 |
except Exception as e:
|
| 19 |
+
answer = f"Error: {e}"
|
| 20 |
+
|
| 21 |
+
history.append({"role": "user", "content": message})
|
| 22 |
+
history.append({"role": "assistant", "content": answer})
|
| 23 |
+
|
| 24 |
+
# Display format for Gradio Chatbot UI
|
| 25 |
+
display = [(h["content"] if h["role"]=="user" else None,
|
| 26 |
+
h["content"] if h["role"]=="assistant" else None)
|
| 27 |
+
for h in history if h["role"] in ["user","assistant"]]
|
| 28 |
+
|
| 29 |
+
return display, history
|
| 30 |
+
|
| 31 |
+
HF_IMAGE_MODEL = "stabilityai/stable-diffusion-2-1"
|
| 32 |
+
HF_URL = f"https://api-inference.huggingface.co/models/{HF_IMAGE_MODEL}"
|
| 33 |
+
HDRS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 34 |
|
|
|
|
| 35 |
def generate_image(prompt):
|
|
|
|
|
|
|
| 36 |
payload = {"inputs": prompt, "options": {"wait_for_model": True}}
|
| 37 |
+
r = requests.post(HF_URL, headers=HDRS, json=payload)
|
| 38 |
+
|
|
|
|
| 39 |
img = Image.open(io.BytesIO(r.content)).convert("RGB")
|
| 40 |
return img
|
| 41 |
|
|
|
|
| 42 |
with gr.Blocks() as app:
|
| 43 |
+
gr.Markdown("<h1 style='text-align:center
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|