Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import base64
|
| 4 |
+
import requests
|
| 5 |
+
import json
|
| 6 |
+
from groq import Groq
|
| 7 |
+
|
| 8 |
+
# Load Groq API key from environment (Gradio Spaces -> Secrets)
|
| 9 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 10 |
+
if not GROQ_API_KEY:
|
| 11 |
+
raise ValueError("GROQ_API_KEY not found. Please add it as a secret in your Hugging Face Space.")
|
| 12 |
+
|
| 13 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 14 |
+
|
| 15 |
+
# Function to handle image upload and Groq API call
|
| 16 |
+
def generate_catalog(image):
|
| 17 |
+
if image is None:
|
| 18 |
+
return "", "", "", ""
|
| 19 |
+
|
| 20 |
+
# Convert uploaded image to base64
|
| 21 |
+
mime_type = "image/jpeg"
|
| 22 |
+
with open(image.name, "rb") as f:
|
| 23 |
+
encoded_image = base64.b64encode(f.read()).decode("utf-8")
|
| 24 |
+
image_data = f"data:{mime_type};base64,{encoded_image}"
|
| 25 |
+
|
| 26 |
+
# Prompt
|
| 27 |
+
prompt = (
|
| 28 |
+
"You are an expert e-commerce assistant. Given a product image, generate the following in JSON format:\n"
|
| 29 |
+
"- A short, catchy Title (max 8 words)\n"
|
| 30 |
+
"- A full Description (100–150 words)\n"
|
| 31 |
+
"- 5–7 SEO Keywords\n"
|
| 32 |
+
"- 5–7 relevant Tags\n\n"
|
| 33 |
+
"Output only in JSON with keys: title, description, keywords, tags."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Groq API call
|
| 37 |
+
try:
|
| 38 |
+
response = client.chat.completions.create(
|
| 39 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 40 |
+
messages=[
|
| 41 |
+
{
|
| 42 |
+
"role": "user",
|
| 43 |
+
"content": [
|
| 44 |
+
{"type": "text", "text": prompt},
|
| 45 |
+
{"type": "image_url", "image_url": {"url": image_data}},
|
| 46 |
+
],
|
| 47 |
+
}
|
| 48 |
+
],
|
| 49 |
+
response_format={"type": "json_object"},
|
| 50 |
+
max_completion_tokens=1024,
|
| 51 |
+
temperature=0.5,
|
| 52 |
+
top_p=1,
|
| 53 |
+
stream=False,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
parsed = json.loads(response.choices[0].message.content)
|
| 57 |
+
title = parsed.get("title", "")
|
| 58 |
+
description = parsed.get("description", "")
|
| 59 |
+
keywords = ", ".join(parsed.get("keywords", []))
|
| 60 |
+
tags = ", ".join(parsed.get("tags", []))
|
| 61 |
+
return title, description, keywords, tags
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return "Error", str(e), "", ""
|
| 65 |
+
|
| 66 |
+
# Gradio UI
|
| 67 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as app:
|
| 68 |
+
gr.Markdown("""
|
| 69 |
+
# 🛒 Instant Product Catalog Assistant
|
| 70 |
+
Upload a product image and instantly get a title, description, keywords, and tags!
|
| 71 |
+
""")
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
image_input = gr.File(label="Upload Product Image", type="filepath")
|
| 75 |
+
submit_btn = gr.Button("Generate Catalog")
|
| 76 |
+
|
| 77 |
+
output_title = gr.Textbox(label="Title")
|
| 78 |
+
output_description = gr.Textbox(label="Description", lines=5)
|
| 79 |
+
output_keywords = gr.Textbox(label="Keywords (comma separated)")
|
| 80 |
+
output_tags = gr.Textbox(label="Tags (comma separated)")
|
| 81 |
+
|
| 82 |
+
submit_btn.click(generate_catalog, inputs=[image_input], outputs=[output_title, output_description, output_keywords, output_tags])
|
| 83 |
+
|
| 84 |
+
app.launch()
|