Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,40 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
def generate_image(prompt):
|
| 7 |
-
API_URL = "https://api-inference.huggingface.co/models/
|
| 8 |
-
API_TOKEN = os.getenv("HF_READ_TOKEN") #
|
| 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
|
| 19 |
if response.status_code != 200:
|
| 20 |
return f"Error: {response.status_code}, {response.text}"
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
return
|
| 28 |
|
| 29 |
# Define the chatbot function to return the generated image
|
| 30 |
def chatbot(prompt):
|
| 31 |
image = generate_image(prompt)
|
| 32 |
return image
|
| 33 |
|
| 34 |
-
# Create the Gradio interface
|
| 35 |
interface = gr.Interface(
|
| 36 |
fn=chatbot,
|
| 37 |
inputs="text",
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
|
| 7 |
+
# Function to generate image from Hugging Face API
|
| 8 |
def generate_image(prompt):
|
| 9 |
+
API_URL = "https://api-inference.huggingface.co/models/prompthero/openjourney"
|
| 10 |
+
API_TOKEN = os.getenv("HF_READ_TOKEN") # Make sure the token is in your environment
|
| 11 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 12 |
|
| 13 |
payload = {
|
| 14 |
+
"inputs": prompt,
|
| 15 |
}
|
| 16 |
|
| 17 |
# Call the Hugging Face API to generate the image
|
| 18 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 19 |
|
| 20 |
+
# Check if the response was successful
|
| 21 |
if response.status_code != 200:
|
| 22 |
return f"Error: {response.status_code}, {response.text}"
|
| 23 |
|
| 24 |
+
# Ensure the response contains an image by loading it into PIL
|
| 25 |
+
try:
|
| 26 |
+
image = Image.open(BytesIO(response.content))
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error processing image: {str(e)}"
|
| 29 |
|
| 30 |
+
return image # Return the PIL image object
|
| 31 |
|
| 32 |
# Define the chatbot function to return the generated image
|
| 33 |
def chatbot(prompt):
|
| 34 |
image = generate_image(prompt)
|
| 35 |
return image
|
| 36 |
|
| 37 |
+
# Create the Gradio interface
|
| 38 |
interface = gr.Interface(
|
| 39 |
fn=chatbot,
|
| 40 |
inputs="text",
|