Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,97 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
import base64
|
|
|
|
| 4 |
import re
|
| 5 |
-
from PIL import Image
|
| 6 |
from groq import Groq
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
return None
|
| 15 |
-
buffered = io.BytesIO()
|
| 16 |
-
image.save(buffered, format="JPEG")
|
| 17 |
-
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 18 |
-
|
| 19 |
-
def feifeichat(input_img):
|
| 20 |
-
if input_img is None:
|
| 21 |
-
return '{"error": "No image provided"}'
|
| 22 |
-
|
| 23 |
-
base64_image = encode_image(input_img)
|
| 24 |
-
|
| 25 |
-
prompt = """Analyze the provided image in detail. Generate a structured response adhering strictly to the following criteria:
|
| 26 |
-
1. Provide a concise, clear title summarizing the main subject.
|
| 27 |
-
2. Provide a detailed, engaging description explaining the content, context, key elements, colors, and features visible in the image.
|
| 28 |
-
3. Return the response strictly as valid raw JSON with NO markdown blocks or outer conversational text.
|
| 29 |
-
|
| 30 |
-
Required JSON format:
|
| 31 |
-
{
|
| 32 |
-
"title": "Short descriptive title",
|
| 33 |
-
"description": "Comprehensive detailed description of the image content..."
|
| 34 |
-
}"""
|
| 35 |
|
|
|
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# Strip thinking
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
-
gr.Markdown("
|
| 71 |
-
with gr.
|
| 72 |
-
input_img = gr.Image(
|
| 73 |
-
output_text = gr.Textbox(label="
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
submit_btn.click(
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
from io import BytesIO
|
| 3 |
import base64
|
| 4 |
+
import os
|
| 5 |
import re
|
|
|
|
| 6 |
from groq import Groq
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
# Read the key name from Hugging Face Environment Variables secrets configuration
|
| 10 |
+
api_key = os.getenv("GROQ_API_KEY", "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")
|
| 11 |
+
client = Groq(api_key=api_key)
|
| 12 |
+
|
| 13 |
+
def encode_image(image_path):
|
| 14 |
+
"""Encode the image to base64."""
|
| 15 |
+
try:
|
| 16 |
+
image = Image.open(image_path).convert("RGB")
|
| 17 |
+
|
| 18 |
+
# Resize the image to a height of 512 while maintaining aspect ratio
|
| 19 |
+
base_height = 512
|
| 20 |
+
h_percent = (base_height / float(image.size[1]))
|
| 21 |
+
w_size = int((float(image.size[0]) * float(h_percent)))
|
| 22 |
+
image = image.resize((w_size, base_height), Image.LANCZOS)
|
| 23 |
+
|
| 24 |
+
buffered = BytesIO()
|
| 25 |
+
image.save(buffered, format="JPEG")
|
| 26 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 27 |
|
| 28 |
+
return img_str
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error encoding image: {e}")
|
| 31 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
def feifeichat(image):
|
| 34 |
try:
|
| 35 |
+
if image is None:
|
| 36 |
+
return "Please upload a photo"
|
| 37 |
+
|
| 38 |
+
base64_image = encode_image(image)
|
| 39 |
+
if not base64_image:
|
| 40 |
+
return "Error processing image"
|
| 41 |
+
|
| 42 |
+
messages = [
|
| 43 |
+
{
|
| 44 |
+
"role": "user",
|
| 45 |
+
"content": [
|
| 46 |
+
{
|
| 47 |
+
"type": "text",
|
| 48 |
+
"text": "Analyze the image and return strictly valid JSON format with keys 'title', 'description', 'meta_title' (max 60 chars), 'meta_description' (max 155 chars), 'tags' (array of 5-8 strings), 'style_breakdown' (object with keys: aesthetic, top, bottom, legwear, accessories), and 'faqs' (array of two objects with 'q' and 'a'). Follow these strict copywriting guidelines: 1. Description must be an elegant, descriptive paragraph (3-4 sentences) written like an alternative fashion lookbook. 2. Focus on physical aesthetics: mention terms like 'slender body', 'petite frame', 'youthful appeal', 'cute vibe', and 'sexy style' naturally. 3. Avoid literal camera actions: DO NOT say 'taking a selfie', 'mirror picture', or 'holding a phone'. Describe the pose as casual, relaxed, or playful. 4. Tags must be individual aesthetic search terms (e.g., ['egirl-aesthetic', 'thigh-highs', 'white-tights', 'alternative-fashion'])."
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"type": "image_url",
|
| 52 |
+
"image_url": {
|
| 53 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
| 54 |
}
|
| 55 |
+
}
|
| 56 |
+
]
|
| 57 |
+
}
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
response = client.chat.completions.create(
|
| 61 |
+
model="qwen/qwen3.6-27b",
|
| 62 |
+
messages=messages,
|
| 63 |
+
response_format={"type": "json_object"},
|
| 64 |
+
temperature=0.7,
|
| 65 |
+
stream=False
|
| 66 |
)
|
| 67 |
+
|
| 68 |
+
raw_content = response.choices[0].message.content or ""
|
| 69 |
+
|
| 70 |
+
# Strip Qwen reasoning/thinking blocks (<think>...</think>)
|
| 71 |
+
cleaned = re.sub(r'<think>.*?</think>', '', raw_content, flags=re.DOTALL).strip()
|
| 72 |
+
|
| 73 |
+
# Strip markdown fences if present
|
| 74 |
+
cleaned = re.sub(r'^```(?:json)?\s*', '', cleaned)
|
| 75 |
+
cleaned = re.sub(r'\s*```$', '', cleaned)
|
| 76 |
+
|
| 77 |
+
return cleaned.strip()
|
| 78 |
+
|
| 79 |
except Exception as e:
|
| 80 |
+
print(f"Error: {e}")
|
| 81 |
+
return f"An error occurred while processing your request: {e}"
|
| 82 |
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
+
gr.Markdown("Image To Flux Prompt")
|
| 85 |
+
with gr.Tab(label="Image To Flux Prompt"):
|
| 86 |
+
input_img = gr.Image(label="Input Picture", height=320, type="filepath")
|
| 87 |
+
output_text = gr.Textbox(label="Flux Prompt")
|
| 88 |
+
submit_btn = gr.Button(value="Submit")
|
| 89 |
+
|
| 90 |
+
submit_btn.click(
|
| 91 |
+
fn=feifeichat,
|
| 92 |
+
inputs=input_img,
|
| 93 |
+
outputs=output_text,
|
| 94 |
+
api_name="feifeichat"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
demo.launch()
|