Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,24 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# Initialize the HuggingFace Inference Client with the specified model
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
def format_prompt(
|
8 |
system_prompt = """
|
9 |
You are an advanced language model designed to create detailed and creative image prompts for logo generation. Based on the user's input, generate an elaborate and descriptive image prompt that can be used to create a high-quality logo. Ensure that the prompt is clear, imaginative, and provides specific details that will guide the logo creation process effectively.
|
10 |
"""
|
11 |
prompt = f"<s>[SYS] {system_prompt} [/SYS][INST] {logo_request} [/INST]</s>"
|
12 |
return prompt
|
13 |
|
14 |
-
def
|
15 |
temperature = float(temperature)
|
16 |
if temperature < 1e-2:
|
17 |
temperature = 1e-2
|
@@ -26,14 +33,40 @@ def generate(logo_request, temperature=0.9, max_new_tokens=512, top_p=0.95, repe
|
|
26 |
"seed": 42,
|
27 |
}
|
28 |
|
29 |
-
formatted_prompt = format_prompt(
|
30 |
-
stream =
|
31 |
output = ""
|
32 |
|
33 |
for response in stream:
|
34 |
output += response.token.text
|
35 |
yield output
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
css = """
|
38 |
#mkd {
|
39 |
height: 500px;
|
@@ -52,7 +85,7 @@ with gr.Blocks(css=css) as gpt:
|
|
52 |
generate_button = gr.Button("Generate")
|
53 |
output_area = gr.Textbox(label="AI Response", interactive=False, lines=10)
|
54 |
generate_button.click(
|
55 |
-
fn=
|
56 |
inputs=[logo_input],
|
57 |
outputs=output_area
|
58 |
)
|
@@ -60,7 +93,7 @@ with gr.Blocks(css=css) as gpt:
|
|
60 |
gr.Markdown("""
|
61 |
---
|
62 |
### Meta Information
|
63 |
-
**Project Title**: Logo
|
64 |
|
65 |
**Github**: [https://github.com/pacnimo/gpt-prompt-generator](https://github.com/pacnimo/)
|
66 |
|
@@ -70,3 +103,23 @@ with gr.Blocks(css=css) as gpt:
|
|
70 |
""") # Meta, project description, and footer added here
|
71 |
|
72 |
gpt.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from gradio_client import Client
|
4 |
+
from PIL import Image
|
5 |
+
import requests
|
6 |
+
from io import BytesIO
|
7 |
+
import streamlit as st
|
8 |
|
9 |
# Initialize the HuggingFace Inference Client with the specified model
|
10 |
+
client_mistral = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
11 |
+
# Initialize the Playground AI client
|
12 |
+
client_playground = Client("https://playgroundai-playground-v2-5.hf.space/--replicas/c9ozb/")
|
13 |
|
14 |
+
def format_prompt(logo_request):
|
15 |
system_prompt = """
|
16 |
You are an advanced language model designed to create detailed and creative image prompts for logo generation. Based on the user's input, generate an elaborate and descriptive image prompt that can be used to create a high-quality logo. Ensure that the prompt is clear, imaginative, and provides specific details that will guide the logo creation process effectively.
|
17 |
"""
|
18 |
prompt = f"<s>[SYS] {system_prompt} [/SYS][INST] {logo_request} [/INST]</s>"
|
19 |
return prompt
|
20 |
|
21 |
+
def generate_improved_prompt(logo_request, temperature=0.9, max_new_tokens=512, top_p=0.95, repetition_penalty=1.0):
|
22 |
temperature = float(temperature)
|
23 |
if temperature < 1e-2:
|
24 |
temperature = 1e-2
|
|
|
33 |
"seed": 42,
|
34 |
}
|
35 |
|
36 |
+
formatted_prompt = format_prompt(logo_request)
|
37 |
+
stream = client_mistral.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
38 |
output = ""
|
39 |
|
40 |
for response in stream:
|
41 |
output += response.token.text
|
42 |
yield output
|
43 |
|
44 |
+
def generate_image(prompt, negative_prompt="", use_negative_prompt=False, seed=0, width=1024, height=1024, guidance_scale=7.5, randomize_seed=True):
|
45 |
+
result = client_playground.predict(
|
46 |
+
prompt,
|
47 |
+
negative_prompt,
|
48 |
+
use_negative_prompt,
|
49 |
+
seed,
|
50 |
+
width,
|
51 |
+
height,
|
52 |
+
guidance_scale,
|
53 |
+
randomize_seed,
|
54 |
+
api_name="/run"
|
55 |
+
)
|
56 |
+
|
57 |
+
# Extract the image URL from the result
|
58 |
+
image_path = result[0][0]["image"]
|
59 |
+
image_url = "https://playgroundai-playground-v2-5.hf.space/--replicas/c9ozb/file=" + image_path
|
60 |
+
|
61 |
+
# Fetch and display the result image
|
62 |
+
response = requests.get(image_url)
|
63 |
+
|
64 |
+
if response.headers['Content-Type'].startswith('image'):
|
65 |
+
img = Image.open(BytesIO(response.content))
|
66 |
+
return img
|
67 |
+
else:
|
68 |
+
return None
|
69 |
+
|
70 |
css = """
|
71 |
#mkd {
|
72 |
height: 500px;
|
|
|
85 |
generate_button = gr.Button("Generate")
|
86 |
output_area = gr.Textbox(label="AI Response", interactive=False, lines=10)
|
87 |
generate_button.click(
|
88 |
+
fn=generate_improved_prompt,
|
89 |
inputs=[logo_input],
|
90 |
outputs=output_area
|
91 |
)
|
|
|
93 |
gr.Markdown("""
|
94 |
---
|
95 |
### Meta Information
|
96 |
+
**Project Title**: Magic AI Website Logo Creator
|
97 |
|
98 |
**Github**: [https://github.com/pacnimo/gpt-prompt-generator](https://github.com/pacnimo/)
|
99 |
|
|
|
103 |
""") # Meta, project description, and footer added here
|
104 |
|
105 |
gpt.launch(debug=True)
|
106 |
+
|
107 |
+
st.title("Image Generation using API")
|
108 |
+
|
109 |
+
# Streamlit app input fields
|
110 |
+
prompt = st.text_input("Enter prompt:", "")
|
111 |
+
negative_prompt = st.text_input("Enter negative prompt:", "")
|
112 |
+
use_negative_prompt = st.checkbox("Use negative prompt", False)
|
113 |
+
seed = st.number_input("Seed (0-2147483647):", value=0, min_value=0, max_value=2147483647)
|
114 |
+
width = st.slider("Width:", 1024, 1536)
|
115 |
+
height = st.slider("Height:", 1024, 1536)
|
116 |
+
guidance_scale = st.slider("Guidance Scale:", 3.0, 20.0, 0.1)
|
117 |
+
randomize_seed = st.checkbox("Randomize seed", True)
|
118 |
+
|
119 |
+
if st.button("Generate Image"):
|
120 |
+
with st.spinner("Generating image..."):
|
121 |
+
img = generate_image(prompt, negative_prompt, use_negative_prompt, seed, width, height, guidance_scale, randomize_seed)
|
122 |
+
if img:
|
123 |
+
st.image(img, caption="Generated Image")
|
124 |
+
else:
|
125 |
+
st.error("The content retrieved is not an image.")
|