File size: 1,359 Bytes
14149d2
fd68473
14149d2
fd68473
c26930a
 
 
14149d2
fd68473
15c32ad
 
 
fd68473
 
15c32ad
 
 
fd68473
15c32ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd68473
14149d2
 
15c32ad
 
14149d2
 
 
 
15c32ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os, io, time, requests, gradio as gr
from PIL import Image
import google.generativeai as genai

HF_TOKEN = os.getenv("HF_API_TOKEN")
GEMINI_KEY = os.getenv("GEMINI_API_KEY")

genai.configure(api_key=GEMINI_KEY)

def chat_fn(message, history):
    if not history:
        history = []

    try:
        model = genai.GenerativeModel("gemini-pro")
        response = model.generate_content(message)
        answer = response.text
    except Exception as e:
        answer = f"Error: {e}"

    history.append({"role": "user", "content": message})
    history.append({"role": "assistant", "content": answer})

    # Display format for Gradio Chatbot UI
    display = [(h["content"] if h["role"]=="user" else None,
                h["content"] if h["role"]=="assistant" else None)
               for h in history if h["role"] in ["user","assistant"]]

    return display, history

HF_IMAGE_MODEL = "stabilityai/stable-diffusion-2-1"
HF_URL = f"https://api-inference.huggingface.co/models/{HF_IMAGE_MODEL}"
HDRS = {"Authorization": f"Bearer {HF_TOKEN}"}

def generate_image(prompt):
    payload = {"inputs": prompt, "options": {"wait_for_model": True}}
    r = requests.post(HF_URL, headers=HDRS, json=payload)

    img = Image.open(io.BytesIO(r.content)).convert("RGB")
    return img

with gr.Blocks() as app:
    gr.Markdown("<h1 style='text-align:center