randeom's picture
Update app.py
2db96fd verified
raw
history blame
4.4 kB
import gradio as gr
from huggingface_hub import InferenceClient
from gradio_client import Client
from PIL import Image
import requests
from io import BytesIO
# Initialize the HuggingFace Inference Client with the specified model
client_mistral = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
# Initialize the Playground AI client
client_playground = Client("https://playgroundai-playground-v2-5.hf.space/--replicas/c9ozb/")
def format_prompt(logo_request):
system_prompt = """
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.
"""
prompt = f"<s>[SYS] {system_prompt} [/SYS][INST] {logo_request} [/INST]</s>"
return prompt
def generate_improved_prompt(logo_request, temperature=0.9, max_new_tokens=512, top_p=0.95, repetition_penalty=1.0):
temperature = float(temperature)
if temperature < 1e-2:
temperature = 1e-2
top_p = float(top_p)
generate_kwargs = {
"temperature": temperature,
"max_new_tokens": max_new_tokens,
"top_p": top_p,
"repetition_penalty": repetition_penalty,
"do_sample": True,
"seed": 42,
}
formatted_prompt = format_prompt(logo_request)
stream = client_mistral.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
output = ""
for response in stream:
output += response.token.text
return output
def generate_image(prompt, width=1024, height=1024, guidance_scale=7.5):
result = client_playground.predict(
prompt,
"", # negative prompt
False, # use negative prompt
0, # seed
width, # width
height, # height
guidance_scale, # guidance scale
True, # randomize seed
api_name="/run"
)
# Extract the image URL from the result
image_path = result[0][0]["image"]
image_url = "https://playgroundai-playground-v2-5.hf.space/--replicas/c9ozb/file=" + image_path
# Fetch and display the result image
response = requests.get(image_url)
if response.headers['Content-Type'].startswith('image'):
img = Image.open(BytesIO(response.content))
return img
else:
return None
css = """
#mkd {
height: 500px;
overflow: auto;
border: 1px solid #ccc;
}
"""
def process_request(logo_request, width, height, guidance_scale):
improved_prompt = generate_improved_prompt(logo_request)
image = generate_image(improved_prompt, width, height, guidance_scale)
return improved_prompt, image
with gr.Blocks(css=css) as app:
with gr.Row():
with gr.Column(scale=2):
gr.HTML("<h1>Settings</h1>")
logo_input = gr.Textbox(label="Input your logo request", placeholder="Describe the logo you want...")
width = gr.Slider(label="Width", minimum=256, maximum=1536, value=1024)
height = gr.Slider(label="Height", minimum=256, maximum=1536, value=1024)
guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=20, value=7.5)
with gr.Column(scale=3):
gr.HTML("<h1><center>Logo Prompt Generator<h1><center>")
generate_button = gr.Button("Generate")
prompt_output = gr.Textbox(label="Generated Prompt", interactive=False, lines=5)
image_output = gr.Image(label="Generated Image")
generate_button.click(
fn=process_request,
inputs=[logo_input, width, height, guidance_scale],
outputs=[prompt_output, image_output]
)
gr.Markdown("""
---
### Meta Information
**Project Title**: Logo Prompt Generator
**Github**: [https://github.com/pacnimo/gpt-prompt-generator](https://github.com/pacnimo/)
**Description**: Logo Prompt Generator is Free and Easy to Use. Create a GPT Prompt Based on the Logo Request. 1 Click Prompt Generator.
**Footer**: © 2024 by [pacnimo](https://github.com/pacnimo/). All rights reserved.
""") # Meta, project description, and footer added here
app.launch(debug=True)