NayabShakeel commited on
Commit
24bb010
verified
1 Parent(s): a52665a

Update test_openai.py

Browse files
Files changed (1) hide show
  1. test_openai.py +28 -16
test_openai.py CHANGED
@@ -1,20 +1,32 @@
1
- import openai
 
 
2
 
3
- # Set your OpenAI API key
4
- openai.api_key = "YOUR_OPENAI_API_KEY" # Replace with your OpenAI API key
 
 
 
 
 
 
 
 
 
 
5
 
6
- # Define a quote
7
- random_text = "The only way to do great work is to love what you do."
 
8
 
9
- # Call OpenAI's DALL路E model to generate an image from the quote
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
- # Extract the image URL from the response
17
- image_url = response['data'][0]['url']
18
-
19
- # Print the image URL
20
- print(f"Image URL: {image_url}")
 
 
 
 
 
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.")