Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ from PIL import Image
|
|
4 |
import gradio as gr
|
5 |
import os
|
6 |
|
7 |
-
# Assuming
|
8 |
ZEPHYR_API_TOKEN = os.getenv("HF_API_TOKEN")
|
9 |
SD_API_TOKEN = os.getenv("HF_API_TOKEN")
|
10 |
|
@@ -17,7 +17,14 @@ SD_API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-dif
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}"}
|
@@ -35,23 +42,23 @@ def generate_image_from_prompt(prompt, guidance_scale=7.5, width=1024, height=76
|
|
35 |
image = Image.open(io.BytesIO(image_bytes))
|
36 |
return image
|
37 |
|
38 |
-
def
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
# Step 2: Use the generated prompt to create an image with Stable Diffusion
|
44 |
if generated_prompt:
|
45 |
-
|
|
|
46 |
else:
|
47 |
raise ValueError("Failed to generate a prompt from the LinkedIn text.")
|
48 |
|
49 |
iface = gr.Interface(
|
50 |
-
fn=
|
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()
|
|
|
4 |
import gradio as gr
|
5 |
import os
|
6 |
|
7 |
+
# Assuming your API tokens are correctly set in environment variables
|
8 |
ZEPHYR_API_TOKEN = os.getenv("HF_API_TOKEN")
|
9 |
SD_API_TOKEN = os.getenv("HF_API_TOKEN")
|
10 |
|
|
|
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 |
+
# Assuming the response is correctly formatted JSON and the first item contains the desired data
|
21 |
+
response_data = response.json()
|
22 |
+
if isinstance(response_data, list) and len(response_data) > 0:
|
23 |
+
# Extracting the generated text from the first item in the response list
|
24 |
+
generated_text = response_data[0].get("generated_text", "")
|
25 |
+
return generated_text
|
26 |
+
else:
|
27 |
+
raise ValueError("Unexpected response format from Zephyr API")
|
28 |
|
29 |
def generate_image_from_prompt(prompt, guidance_scale=7.5, width=1024, height=768, num_inference_steps=30):
|
30 |
headers = {"Authorization": f"Bearer {SD_API_TOKEN}"}
|
|
|
42 |
image = Image.open(io.BytesIO(image_bytes))
|
43 |
return image
|
44 |
|
45 |
+
def generate_image_and_show_prompt(linkedin_text):
|
46 |
+
# Generate a prompt from the LinkedIn text using Zephyr
|
47 |
+
generated_prompt = query_zephyr(linkedin_text)
|
48 |
+
|
49 |
+
# Use the generated prompt to create an image with Stable Diffusion
|
|
|
50 |
if generated_prompt:
|
51 |
+
image = generate_image_from_prompt(generated_prompt)
|
52 |
+
return image, generated_prompt
|
53 |
else:
|
54 |
raise ValueError("Failed to generate a prompt from the LinkedIn text.")
|
55 |
|
56 |
iface = gr.Interface(
|
57 |
+
fn=generate_image_and_show_prompt,
|
58 |
inputs=[gr.Textbox(label="LinkedIn Message", placeholder="Enter LinkedIn message here...")],
|
59 |
+
outputs=[gr.Image(type="pil"), gr.Textbox(label="Generated Prompt")],
|
60 |
title="Generate Images from LinkedIn Messages",
|
61 |
+
description="Enter a LinkedIn message to generate a creative prompt with Zephyr, which is then used to generate an image with Stable Diffusion. The generated prompt is also displayed."
|
62 |
)
|
63 |
|
64 |
iface.launch()
|