QuickNotesAI commited on
Commit
08afba1
·
verified ·
1 Parent(s): d0decf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -34
app.py CHANGED
@@ -1,37 +1,100 @@
1
  import gradio as gr
2
- from diffusers import StableDiffusionPipeline
3
- import torch
4
-
5
- # Model Load (Free Space friendly)
6
- model_id = "runwayml/stable-diffusion-v1-5"
7
- pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
8
- pipe = pipe.to("cpu") # Free Space CPU safe
9
-
10
- # Function for fast low-res preview + high-res paid option
11
- def generate_image(prompt, high_res=False):
12
- if high_res:
13
- # Paid high-res: slower
14
- image = pipe(prompt, height=512, width=512).images[0]
15
- else:
16
- # Free low-res: faster
17
- image = pipe(prompt, height=256, width=256).images[0]
18
- return image
19
-
20
- # Gradio Interface
21
- iface = gr.Interface(
22
- fn=generate_image,
23
- inputs=[
24
- gr.Textbox(lines=2, placeholder="Type your image description here...", label="Prompt"),
25
- gr.Checkbox(label="High-Resolution (Paid Feature)")
26
- ],
27
- outputs=gr.Image(label="Generated Image"),
28
- title="AI Image Generator (Fast Preview)",
29
- description="""
30
- Type any text to get beautiful AI-generated images instantly!
31
- Free low-res images for fast preview.
32
- Check High-Resolution for paid download.
33
- """,
34
- allow_flagging="never"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  )
36
 
37
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+
3
+ # -------------------------
4
+ # SMART UNDERSTANDING LOGIC
5
+ # -------------------------
6
+
7
+ def detect_mode(user_text):
8
+ text = user_text.lower()
9
+
10
+ if any(k in text for k in ["upsc", "exam", "notes", "school", "college"]):
11
+ return "academic"
12
+ if any(k in text for k in ["instagram", "facebook", "dp", "profile", "post"]):
13
+ return "social"
14
+ if any(k in text for k in ["diagram", "flow", "map"]):
15
+ return "diagram"
16
+ return "general"
17
+
18
+
19
+ def build_prompt(user_text):
20
+ mode = detect_mode(user_text)
21
+
22
+ base_realism = (
23
+ "highly realistic, photorealistic image, "
24
+ "natural lighting, real human texture, "
25
+ "no cartoon, no fantasy, professional quality"
26
+ )
27
+
28
+ if mode == "academic":
29
+ return (
30
+ f"{base_realism}, academic textbook style, "
31
+ f"exam-safe visual, subject based on: {user_text}"
32
+ )
33
+
34
+ if mode == "social":
35
+ return (
36
+ f"{base_realism}, premium social media portrait, "
37
+ f"clean background, instagram facebook ready, "
38
+ f"based on: {user_text}"
39
+ )
40
+
41
+ if mode == "diagram":
42
+ return (
43
+ f"clean academic diagram, simple design, "
44
+ f"clear labels, NCERT style, topic: {user_text}"
45
+ )
46
+
47
+ return f"{base_realism}, based on: {user_text}"
48
+
49
+
50
+ NEGATIVE_PROMPT = (
51
+ "cartoon, anime, illustration, painting, fantasy, unreal, "
52
+ "plastic skin, blurry, distorted, watermark, text, logo"
53
  )
54
 
55
+ # -------------------------
56
+ # IMAGE GENERATION FUNCTION
57
+ # -------------------------
58
+ def generate_image(user_text):
59
+ final_prompt = build_prompt(user_text)
60
+
61
+ # 🔴 यहां अपना MODEL / API call लगाना है
62
+ # image = model.generate(
63
+ # prompt=final_prompt,
64
+ # negative_prompt=NEGATIVE_PROMPT
65
+ # )
66
+ # return image
67
+
68
+ return None # placeholder (UI test के लिए)
69
+
70
+ # -------------------------
71
+ # GRADIO UI
72
+ # -------------------------
73
+ with gr.Blocks(title="Smart Text-to-Art AI") as demo:
74
+ gr.Markdown(
75
+ """
76
+ ## 🎓 Smart Text-to-Art AI Generator
77
+ बस लिखो, AI खुद समझेगा और realistic image बनाएगा
78
+
79
+ ✔ Student Safe
80
+ ✔ Social Media Ready
81
+ ✔ No Cartoon / No Fantasy
82
+ """
83
+ )
84
+
85
+ user_input = gr.Textbox(
86
+ label="आप क्या बनवाना चाहते हैं?",
87
+ placeholder="Example: UPSC ke liye raja ki realistic photo"
88
+ )
89
+
90
+ generate_btn = gr.Button("Generate Image")
91
+ output = gr.Image(label="Output", type="pil")
92
+
93
+ generate_btn.click(
94
+ fn=generate_image,
95
+ inputs=user_input,
96
+ outputs=output
97
+ )
98
+
99
+ if __name__ == "__main__":
100
+ demo.launch()