Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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
|
| 26 |
-
# ==============================
|
| 27 |
def generate_caption(api_key, image, style):
|
| 28 |
try:
|
| 29 |
-
|
| 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 |
-
#
|
| 42 |
inputs = processor(image, return_tensors="pt")
|
| 43 |
output = model.generate(**inputs, max_new_tokens=30)
|
| 44 |
-
basic_caption = processor.decode(
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
#
|
| 47 |
client = Groq(api_key=api_key)
|
| 48 |
|
| 49 |
prompt = f"""
|
| 50 |
-
Rewrite
|
| 51 |
-
Keep it short (1β2 lines)
|
| 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 |
-
|
| 66 |
|
| 67 |
return (
|
| 68 |
f"πΌοΈ **Basic Caption:** {basic_caption}\n\n"
|
| 69 |
-
f"β¨ **AI Refined Caption ({style}):**\n{
|
| 70 |
)
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
-
|
| 74 |
-
return f"β Error occurred:\n{str(e)}"
|
| 75 |
|
| 76 |
-
# ==============================
|
| 77 |
-
#
|
| 78 |
-
# ==============================
|
| 79 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 80 |
gr.Markdown("""
|
| 81 |
-
# πΌοΈ Image Caption Generator
|
| 82 |
-
|
| 83 |
-
|
| 84 |
""")
|
| 85 |
|
| 86 |
-
|
| 87 |
label="π Groq API Key",
|
| 88 |
type="password",
|
| 89 |
-
placeholder="
|
| 90 |
)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
type="pil",
|
| 94 |
-
label="π· Upload Image"
|
| 95 |
-
)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
value="Normal",
|
| 100 |
label="π¨ Caption Style"
|
| 101 |
)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
output_box = gr.Markdown()
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
inputs=[
|
| 110 |
-
outputs=
|
| 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()
|