Ashiii01 commited on
Commit
bd04394
Β·
verified Β·
1 Parent(s): 8396d45

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =====================================
2
+ # INSTALL DEPENDENCIES
3
+ # =====================================
4
+ !pip install gradio transformers pillow torch groq --quiet
5
+
6
+ # =====================================
7
+ # IMPORTS
8
+ # =====================================
9
+ import gradio as gr
10
+ from PIL import Image
11
+ from transformers import BlipProcessor, BlipForConditionalGeneration
12
+ from groq import Groq
13
+ import traceback
14
+
15
+ # =====================================
16
+ # LOAD IMAGE CAPTION MODEL
17
+ # =====================================
18
+ processor = BlipProcessor.from_pretrained(
19
+ "Salesforce/blip-image-captioning-base"
20
+ )
21
+ model = BlipForConditionalGeneration.from_pretrained(
22
+ "Salesforce/blip-image-captioning-base"
23
+ )
24
+
25
+ # =====================================
26
+ # CORE FUNCTION (FIXED)
27
+ # =====================================
28
+ def generate_caption(api_key, image, style):
29
+ try:
30
+ # API KEY CHECK
31
+ if api_key is None or api_key.strip() == "":
32
+ return "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"
33
+
34
+ # IMAGE CHECK
35
+ if image is None:
36
+ return "❌ Please upload an image."
37
+
38
+ # Ensure image is RGB
39
+ if image.mode != "RGB":
40
+ image = image.convert("RGB")
41
+
42
+ # Step 1: Generate base caption
43
+ inputs = processor(image, return_tensors="pt")
44
+ output = model.generate(**inputs, max_new_tokens=30)
45
+ basic_caption = processor.decode(output[0], skip_special_tokens=True)
46
+
47
+ # Step 2: Call Groq API
48
+ client = Groq(api_key=api_key)
49
+
50
+ prompt = f"""
51
+ Rewrite this image caption in a {style.lower()} style.
52
+ Keep it short (1–2 lines) and friendly.
53
+
54
+ Caption:
55
+ "{basic_caption}"
56
+ """
57
+
58
+ response = client.chat.completions.create(
59
+ model="llama-3.3-70b-versatile",
60
+ messages=[
61
+ {"role": "user", "content": prompt}
62
+ ],
63
+ temperature=0.7,
64
+ )
65
+
66
+ refined_caption = response.choices[0].message.content
67
+
68
+ return (
69
+ f"πŸ–ΌοΈ **Basic Caption:** {basic_caption}\n\n"
70
+ f"✨ **AI Refined Caption ({style}):**\n{refined_caption}"
71
+ )
72
+
73
+ except Exception as e:
74
+ # Show real error instead of silent fail
75
+ return f"❌ Error occurred:\n{str(e)}"
76
+
77
+ # =====================================
78
+ # GRADIO UI
79
+ # =====================================
80
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
81
+ gr.Markdown("""
82
+ # πŸ–ΌοΈ Image Caption Generator App
83
+ **Upload image β†’ Generate caption β†’ Refine with Groq AI**
84
+ Beginner-friendly β€’ API-based β€’ Fixed & Working βœ…
85
+ """)
86
+
87
+ api_key_input = gr.Textbox(
88
+ label="πŸ”‘ Groq API Key",
89
+ type="password",
90
+ placeholder="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"
91
+ )
92
+
93
+ image_input = gr.Image(
94
+ type="pil",
95
+ label="πŸ“· Upload Image"
96
+ )
97
+
98
+ style_input = gr.Dropdown(
99
+ choices=["Normal", "Creative", "Fun / Gen-Z"],
100
+ value="Normal",
101
+ label="🎨 Caption Style"
102
+ )
103
+
104
+ generate_btn = gr.Button("πŸš€ Generate Caption")
105
+
106
+ output_box = gr.Markdown()
107
+
108
+ generate_btn.click(
109
+ fn=generate_caption,
110
+ inputs=[api_key_input, image_input, style_input],
111
+ outputs=output_box
112
+ )
113
+
114
+ gr.Markdown("""
115
+ ---
116
+ ⚠️ **Educational AI Project**
117
+ Uses Image Captioning + Groq LLM API
118
+ """)
119
+
120
+ app.launch()