Spaces:
Sleeping
Sleeping
Create app.py
Browse files
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()
|