Spaces:
Sleeping
Sleeping
Update test_openai.py
Browse files- test_openai.py +28 -16
test_openai.py
CHANGED
@@ -1,20 +1,32 @@
|
|
1 |
-
import
|
|
|
|
|
2 |
|
3 |
-
#
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
8 |
|
9 |
-
|
10 |
-
response = openai.Image.create(
|
11 |
-
prompt=random_text, # Use the random quote as the prompt
|
12 |
-
n=1, # Generate 1 image
|
13 |
-
size="1024x1024" # Image size
|
14 |
-
)
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from PIL import Image
|
3 |
+
import streamlit as st
|
4 |
|
5 |
+
# Function to generate image from Hugging Face API (or other providers like DeepAI)
|
6 |
+
def generate_image_from_text(text):
|
7 |
+
response = requests.post(
|
8 |
+
"https://api-inference.huggingface.co/models/dalle-mini",
|
9 |
+
headers={"Authorization": f"Bearer {st.secrets['HF_API_KEY']}"},
|
10 |
+
json={"inputs": text}
|
11 |
+
)
|
12 |
+
if response.status_code == 200:
|
13 |
+
image_url = response.json()[0]['image']
|
14 |
+
return image_url
|
15 |
+
else:
|
16 |
+
return None
|
17 |
|
18 |
+
# Test Image Generation (on button click or any other trigger)
|
19 |
+
if __name__ == "__main__":
|
20 |
+
st.title("Test Image Generation API")
|
21 |
|
22 |
+
user_input = st.text_input("Enter text for image generation:")
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
if st.button("Generate Image"):
|
25 |
+
if user_input:
|
26 |
+
image_url = generate_image_from_text(user_input)
|
27 |
+
if image_url:
|
28 |
+
st.image(image_url, caption="Generated Image")
|
29 |
+
else:
|
30 |
+
st.error("Image generation failed.")
|
31 |
+
else:
|
32 |
+
st.error("Please enter text to generate an image.")
|