concauu commited on
Commit
97dd4d2
·
verified ·
1 Parent(s): 24e8fe4

initial commit

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import io
3
+ from PIL import Image
4
+ import gradio as gr
5
+ from together import Together
6
+ from groq import Groq # Import the Groq library
7
+
8
+ # Initialize the Together and Groq clients with your API keys
9
+ client = Together(api_key="d43d9d8b78242356408329a19a9cdce9290a8c3a254db6293fa6293d207e7e38")
10
+ groq_client = Groq(api_key="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")
11
+
12
+ def enhance_prompt(user_prompt):
13
+ """Enhances the given prompt using Groq and returns the refined prompt."""
14
+ try:
15
+ chat_completion = groq_client.chat.completions.create(
16
+ messages=[
17
+ {
18
+ "role": "system",
19
+ "content": (
20
+ "Create prompts that paint a clear picture for image generation. "
21
+ "Be precise, detailed, and direct, describing not only the content of the image "
22
+ "but also details like tone, style, color palette, and point of view. "
23
+ "For photorealistic images, include the device used (e.g., 'shot on iPhone 16'), "
24
+ "aperture, lens, and shot type. Use clear, visual descriptions. "
25
+ "Prompt Structure: 'A [medium] of [subject], [subject’s characteristics], "
26
+ "[relation to background] [background]. [Details of background] "
27
+ "[Interactions with color and lighting]. (\"Taken on:\"/\"Drawn with:\")[Specific traits of style]' "
28
+ "Include details on medium, subject (clothing, hairstyle, pose, etc.), background, "
29
+ "colors, lighting, style traits, influences, technique, and camera settings if applicable."
30
+ ),
31
+ },
32
+ {"role": "user", "content": user_prompt}
33
+ ],
34
+ model="llama-3.3-70b-versatile",
35
+ temperature=0.5,
36
+ max_completion_tokens=1024,
37
+ top_p=1,
38
+ stop=None,
39
+ stream=False,
40
+ )
41
+ enhanced = chat_completion.choices[0].message.content
42
+ except Exception as e:
43
+ enhanced = f"Error enhancing prompt: {str(e)}"
44
+ return enhanced
45
+
46
+ def generate_image(prompt):
47
+ """Generates an image using the refined prompt."""
48
+ try:
49
+ response = client.images.generate(
50
+ prompt=prompt,
51
+ model="black-forest-labs/FLUX.1-dev-lora",
52
+ width=1024,
53
+ height=768,
54
+ steps=28,
55
+ n=1,
56
+ response_format="url",
57
+ image_loras=[
58
+ {"path": "https://huggingface.co/XLabs-AI/flux-RealismLora", "scale": 1},
59
+ ],
60
+ )
61
+ image_url = response.data[0].url
62
+ image_response = requests.get(image_url)
63
+ image_bytes = image_response.content
64
+ img = Image.open(io.BytesIO(image_bytes))
65
+ except Exception as e:
66
+ # Optionally, handle errors (you can also return a default error image)
67
+ img = None
68
+ return img
69
+
70
+ # Build the Gradio interface with a two-step process
71
+ with gr.Blocks(css=".gradio-container {background-color: #f9f9f9; padding: 20px;}") as demo:
72
+ gr.Markdown("# 2-Step Image Generator")
73
+ gr.Markdown(
74
+ "### Step 1: Prompt Enhancement\n"
75
+ "Enter your original prompt below and click **Enhance Prompt**. "
76
+ "The system will generate a detailed version of your prompt. You can modify the enhanced prompt before generating the image."
77
+ )
78
+
79
+ with gr.Row():
80
+ original_prompt = gr.Textbox(
81
+ label="Your Original Prompt",
82
+ placeholder="Describe your idea here...",
83
+ lines=3
84
+ )
85
+ enhance_button = gr.Button("Enhance Prompt")
86
+
87
+ enhanced_prompt_box = gr.Textbox(
88
+ label="Enhanced Prompt (Editable)",
89
+ placeholder="The enhanced prompt will appear here...",
90
+ lines=3
91
+ )
92
+
93
+ enhance_button.click(fn=enhance_prompt, inputs=original_prompt, outputs=enhanced_prompt_box)
94
+
95
+ gr.Markdown("### Step 2: Image Generation\n"
96
+ "Review and modify the enhanced prompt if necessary. Once you're ready, click **Generate Image** to create your image.")
97
+
98
+ generate_button = gr.Button("Generate Image")
99
+ image_output = gr.Image(label="Generated Image")
100
+
101
+ generate_button.click(fn=generate_image, inputs=enhanced_prompt_box, outputs=image_output)
102
+
103
+ if __name__ == "__main__":
104
+ demo.launch()