Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +50 -39
src/streamlit_app.py
CHANGED
@@ -1,40 +1,51 @@
|
|
1 |
-
import altair as alt
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
import streamlit as st
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import io
|
4 |
+
from PIL import Image
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Load Hugging Face token from environment
|
8 |
+
hf_token = os.environ.get("hf_token")
|
9 |
+
|
10 |
+
# API URLs for different models
|
11 |
+
API_URL_KVI = "https://api-inference.huggingface.co/models/Kvikontent/kviImageR2.0"
|
12 |
+
API_URL_MJ = "https://api-inference.huggingface.co/models/Kvikontent/midjourney-v6"
|
13 |
+
API_URL_DALLE = "https://api-inference.huggingface.co/models/ehristoforu/dalle-3-xl"
|
14 |
+
|
15 |
+
# Headers with Hugging Face token
|
16 |
+
headers = {"Authorization": f"Bearer {hf_token}"}
|
17 |
+
|
18 |
+
# Function to query Hugging Face API
|
19 |
+
def query(payload):
|
20 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
21 |
+
return response.content
|
22 |
+
|
23 |
+
# Streamlit UI
|
24 |
+
st.title("Text To Image models")
|
25 |
+
st.write("Choose model and enter a prompt")
|
26 |
+
|
27 |
+
model = st.selectbox(
|
28 |
+
"Choose model",
|
29 |
+
("KVIImageR2.0", "Midjourney V6", "Dalle 3")
|
30 |
+
)
|
31 |
+
|
32 |
+
prompt = st.text_input("Enter prompt")
|
33 |
+
|
34 |
+
# Generate Image
|
35 |
+
if prompt:
|
36 |
+
if model == "KVIImageR2.0":
|
37 |
+
API_URL = API_URL_KVI
|
38 |
+
elif model == "Midjourney V6":
|
39 |
+
API_URL = API_URL_MJ
|
40 |
+
elif model == "Dalle 3":
|
41 |
+
API_URL = API_URL_DALLE
|
42 |
+
|
43 |
+
image_bytes = query({"inputs": prompt})
|
44 |
+
|
45 |
+
try:
|
46 |
+
image = Image.open(io.BytesIO(image_bytes))
|
47 |
+
st.image(image, caption="Generated Image")
|
48 |
+
st.info("Image generated successfully!")
|
49 |
+
except Exception as e:
|
50 |
+
st.error("Failed to generate image. Please try again.")
|
51 |
+
st.text(str(e))
|