Jangai commited on
Commit
b4b980c
·
verified ·
1 Parent(s): c063430

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -22
app.py CHANGED
@@ -4,15 +4,23 @@ from PIL import Image
4
  import gradio as gr
5
  import os
6
 
7
- # Ensure your API token is correctly set in your environment variables
8
- API_TOKEN = os.getenv("HF_API_TOKEN")
9
- if not API_TOKEN:
10
- raise ValueError("Hugging Face API token not found. Please set the HF_API_TOKEN environment variable.")
11
 
12
- API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
13
- headers = {"Authorization": f"Bearer {API_TOKEN}"}
14
 
15
- def generate_image(prompt, negative_prompt="", guidance_scale=7.5, width=1024, height=768, num_inference_steps=30):
 
 
 
 
 
 
 
 
 
16
  payload = {
17
  "inputs": prompt,
18
  "parameters": {
@@ -22,27 +30,28 @@ def generate_image(prompt, negative_prompt="", guidance_scale=7.5, width=1024, h
22
  "num_inference_steps": num_inference_steps,
23
  },
24
  }
25
- if negative_prompt: # Only add negative_prompt to payload if it's provided
26
- payload["parameters"]["negative_prompt"] = negative_prompt
27
-
28
- response = requests.post(API_URL, headers=headers, json=payload)
29
  image_bytes = response.content
30
  image = Image.open(io.BytesIO(image_bytes))
31
  return image
32
 
 
 
 
 
 
 
 
 
 
 
 
33
  iface = gr.Interface(
34
- fn=generate_image,
35
- inputs=[
36
- gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
37
- gr.Textbox(label="Negative Prompt", placeholder="Enter a negative prompt here (optional)..."),
38
- gr.Slider(label="Guidance Scale", minimum=1, maximum=20, step=0.1, value=7.5),
39
- gr.Slider(label="Width", minimum=768, maximum=1024, step=1, value=1024),
40
- gr.Slider(label="Height", minimum=768, maximum=1024, step=1, value=768),
41
- gr.Slider(label="Number of Inference Steps", minimum=20, maximum=50, step=1, value=30)
42
- ],
43
  outputs=gr.Image(type="pil"),
44
- title="Stable Diffusion XL Image Generator",
45
- description="Generate images with Stable Diffusion XL. Provide a prompt, optionally specify a negative prompt, and adjust other parameters as desired."
46
  )
47
 
48
  iface.launch()
 
4
  import gradio as gr
5
  import os
6
 
7
+ # Assuming you have your API tokens set in environment variables
8
+ ZEPHYR_API_TOKEN = os.getenv("HF_API_TOKEN")
9
+ SD_API_TOKEN = os.getenv("HF_API_TOKEN")
 
10
 
11
+ if not ZEPHYR_API_TOKEN or not SD_API_TOKEN:
12
+ raise ValueError("API tokens not found. Please set the ZEPHYR_API_TOKEN and HF_API_TOKEN environment variables.")
13
 
14
+ ZEPHYR_API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
15
+ SD_API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
16
+
17
+ def query_zephyr(prompt):
18
+ headers = {"Authorization": f"Bearer {ZEPHYR_API_TOKEN}"}
19
+ response = requests.post(ZEPHYR_API_URL, headers=headers, json={"inputs": prompt})
20
+ return response.json()
21
+
22
+ def generate_image_from_prompt(prompt, guidance_scale=7.5, width=1024, height=768, num_inference_steps=30):
23
+ headers = {"Authorization": f"Bearer {SD_API_TOKEN}"}
24
  payload = {
25
  "inputs": prompt,
26
  "parameters": {
 
30
  "num_inference_steps": num_inference_steps,
31
  },
32
  }
33
+ response = requests.post(SD_API_URL, headers=headers, json=payload)
 
 
 
34
  image_bytes = response.content
35
  image = Image.open(io.BytesIO(image_bytes))
36
  return image
37
 
38
+ def generate_image_from_linkedin_text(linkedin_text):
39
+ # Step 1: Generate a prompt from the LinkedIn text using Zephyr
40
+ zephyr_output = query_zephyr(linkedin_text)
41
+ generated_prompt = zephyr_output.get("generated_text", "")
42
+
43
+ # Step 2: Use the generated prompt to create an image with Stable Diffusion
44
+ if generated_prompt:
45
+ return generate_image_from_prompt(generated_prompt)
46
+ else:
47
+ raise ValueError("Failed to generate a prompt from the LinkedIn text.")
48
+
49
  iface = gr.Interface(
50
+ fn=generate_image_from_linkedin_text,
51
+ inputs=[gr.Textbox(label="LinkedIn Message", placeholder="Enter LinkedIn message here...")],
 
 
 
 
 
 
 
52
  outputs=gr.Image(type="pil"),
53
+ title="Generate Images from LinkedIn Messages",
54
+ description="Enter a LinkedIn message to generate a creative prompt with Zephyr, which is then used to generate an image with Stable Diffusion."
55
  )
56
 
57
  iface.launch()