wifix199 commited on
Commit
7f5f8e9
1 Parent(s): c95ed83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -1,31 +1,30 @@
1
- import gradio as gr
2
- import requests
3
  import os
4
- import json
 
5
 
6
- # Define the function that queries the Hugging Face API
7
- def generate_image_from_model(prompt):
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
- # Handle errors
19
  if response.status_code != 200:
20
  return f"Error: {response.status_code}, {response.text}"
21
 
22
- # Assuming the response is an image, save it temporarily and return the path
23
- image_data = response.content
24
  image_path = "generated_image.png"
25
- with open(image_path, "wb") as img_file:
26
- img_file.write(image_data)
27
 
28
- return image_path # Return the path to the image
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):