Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,30 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
import os
|
4 |
-
import
|
|
|
5 |
|
6 |
-
# Define the function
|
7 |
-
def
|
8 |
API_URL = "https://api-inference.huggingface.co/models/prompthero/openjourney"
|
9 |
-
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
10 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
11 |
|
12 |
payload = {
|
13 |
-
"inputs": prompt
|
14 |
}
|
15 |
|
|
|
16 |
response = requests.post(API_URL, headers=headers, json=payload)
|
17 |
|
18 |
-
#
|
19 |
if response.status_code != 200:
|
20 |
return f"Error: {response.status_code}, {response.text}"
|
21 |
|
22 |
-
#
|
23 |
-
image_data = response.content
|
24 |
image_path = "generated_image.png"
|
25 |
-
with open(image_path, "wb") as
|
26 |
-
|
27 |
|
28 |
-
return image_path
|
29 |
|
30 |
# Define the chatbot function to return the generated image
|
31 |
def chatbot(prompt):
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import requests
|
3 |
+
import gradio as gr
|
4 |
|
5 |
+
# Define the function to query Hugging Face API for image generation
|
6 |
+
def generate_image(prompt):
|
7 |
API_URL = "https://api-inference.huggingface.co/models/prompthero/openjourney"
|
8 |
+
API_TOKEN = os.getenv("HF_READ_TOKEN") # Ensure the token is set in your environment
|
9 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
10 |
|
11 |
payload = {
|
12 |
+
"inputs": prompt
|
13 |
}
|
14 |
|
15 |
+
# Call the Hugging Face API to generate the image
|
16 |
response = requests.post(API_URL, headers=headers, json=payload)
|
17 |
|
18 |
+
# Check if the request was successful
|
19 |
if response.status_code != 200:
|
20 |
return f"Error: {response.status_code}, {response.text}"
|
21 |
|
22 |
+
# Save the generated image
|
|
|
23 |
image_path = "generated_image.png"
|
24 |
+
with open(image_path, "wb") as f:
|
25 |
+
f.write(response.content)
|
26 |
|
27 |
+
return image_path
|
28 |
|
29 |
# Define the chatbot function to return the generated image
|
30 |
def chatbot(prompt):
|