File size: 2,192 Bytes
8c6d7b6
4af9859
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42eac23
 
4af9859
 
 
42eac23
4af9859
 
 
 
 
 
 
 
 
42eac23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
import requests
import io
from PIL import Image
import os

# Load Hugging Face token from environment
hf_token = os.environ.get("hf_token")

# API URLs for different models
API_URL_KVI = "https://api-inference.huggingface.co/models/Kvikontent/kviImageR2.0"
API_URL_MJ = "https://api-inference.huggingface.co/models/Kvikontent/midjourney-v6"
API_URL_DALLE = "https://api-inference.huggingface.co/models/ehristoforu/dalle-3-xl"

# Headers with Hugging Face token
headers = {"Authorization": f"Bearer {hf_token}"}

# Function to query Hugging Face API
def query(payload, api_url):
    response = requests.post(api_url, headers=headers, json=payload)
    return response.content

# Streamlit UI
st.title("Text To Image Models")
st.write("Choose model and enter a prompt")

model = st.selectbox(
    "Choose model",
    ("KVIImageR2.0", "Midjourney V6", "Dalle 3")
)

prompt = st.text_input("Enter prompt")

# Button for generating image
if st.button("Generate Image"):
    if prompt:
        if model == "KVIImageR2.0":
            API_URL = API_URL_KVI
        elif model == "Midjourney V6":
            API_URL = API_URL_MJ
        elif model == "Dalle 3":
            API_URL = API_URL_DALLE

        with st.spinner("Generating image... Please wait."):
            image_bytes = query({"inputs": prompt}, API_URL)

            try:
                image = Image.open(io.BytesIO(image_bytes))
                
                # Image preview
                st.image(image, caption="Generated Image Preview", use_column_width=True)

                # Download option
                img_buffer = io.BytesIO()
                image.save(img_buffer, format="PNG")
                st.download_button(
                    label="Download Image",
                    data=img_buffer.getvalue(),
                    file_name="generated_image.png",
                    mime="image/png"
                )

                st.success("Image generated successfully!")
            except Exception as e:
                st.error("Failed to generate image. Please try again.")
                st.text(str(e))
    else:
        st.warning("Please enter a prompt before generating.")