Ashiii01 commited on
Commit
6cb42a5
Β·
verified Β·
1 Parent(s): 0641621

Update app.py

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