AmirAziz1221 commited on
Commit
73d6f9d
·
verified ·
1 Parent(s): b427907

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from groq import Groq
4
+ import torch
5
+ from diffusers import StableDiffusionPipeline
6
+
7
+ # ===== GROQ CLIENT =====
8
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
9
+
10
+ # ===== LOAD STABLE DIFFUSION =====
11
+ model_id = "runwayml/stable-diffusion-v1-5"
12
+
13
+ pipe = StableDiffusionPipeline.from_pretrained(
14
+ model_id,
15
+ torch_dtype=torch.float16
16
+ )
17
+
18
+ pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
19
+
20
+ # ===== PROMPT ENHANCER USING GROQ =====
21
+ def enhance_prompt(user_prompt):
22
+ response = client.chat.completions.create(
23
+ model="llama3-8b-8192",
24
+ messages=[
25
+ {
26
+ "role": "system",
27
+ "content": (
28
+ "You are an expert AI image prompt engineer. "
29
+ "Enhance the user prompt with visual details, lighting, style, realism, "
30
+ "camera angle, and artistic quality. "
31
+ "Return ONLY the enhanced prompt."
32
+ )
33
+ },
34
+ {"role": "user", "content": user_prompt}
35
+ ],
36
+ temperature=0.7,
37
+ max_tokens=150
38
+ )
39
+
40
+ return response.choices[0].message.content.strip()
41
+
42
+ # ===== IMAGE GENERATION =====
43
+ def generate_image(prompt):
44
+ if not prompt.strip():
45
+ return None, ""
46
+
47
+ enhanced_prompt = enhance_prompt(prompt)
48
+
49
+ image = pipe(
50
+ enhanced_prompt,
51
+ num_inference_steps=30,
52
+ guidance_scale=7.5
53
+ ).images[0]
54
+
55
+ return image, enhanced_prompt
56
+
57
+ # ===== UI =====
58
+ with gr.Blocks(theme=gr.themes.Soft(), title="AI Image Generator (Groq + SD)") as demo:
59
+ gr.Markdown(
60
+ """
61
+ # 🎨 AI Image Generator
62
+ **Groq LLM + Stable Diffusion**
63
+ Type a prompt → Get a stunning image
64
+ """
65
+ )
66
+
67
+ prompt_input = gr.Textbox(
68
+ label="Image Prompt",
69
+ placeholder="e.g. A futuristic city at sunset",
70
+ lines=3
71
+ )
72
+
73
+ generate_btn = gr.Button("Generate Image 🚀")
74
+
75
+ image_output = gr.Image(label="Generated Image")
76
+ enhanced_prompt_output = gr.Textbox(
77
+ label="Enhanced Prompt (Generated by Groq)",
78
+ lines=4
79
+ )
80
+
81
+ generate_btn.click(
82
+ generate_image,
83
+ inputs=prompt_input,
84
+ outputs=[image_output, enhanced_prompt_output]
85
+ )
86
+
87
+ gr.Markdown("### 🔹 Demo Prompts (Click & Generate)")
88
+
89
+ demo_prompts = [
90
+ "A cyberpunk city at night",
91
+ "A realistic lion in the jungle",
92
+ "A futuristic robot chef",
93
+ "A fantasy castle in the clouds",
94
+ "A Pakistani truck art style painting"
95
+ ]
96
+
97
+ for demo_text in demo_prompts:
98
+ gr.Button(demo_text).click(
99
+ generate_image,
100
+ inputs=gr.State(demo_text),
101
+ outputs=[image_output, enhanced_prompt_output]
102
+ )
103
+
104
+ demo.launch()