Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,79 @@
|
|
| 1 |
-
from PIL import Image
|
| 2 |
-
from io import BytesIO
|
| 3 |
-
import base64
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from groq import Groq
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 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 |
-
# Open the image file
|
| 17 |
-
image = Image.open(image_path).convert("RGB")
|
| 18 |
-
|
| 19 |
-
# Resize the image to a height of 512 while maintaining the aspect ratio
|
| 20 |
-
base_height = 512
|
| 21 |
-
h_percent = (base_height / float(image.size[1]))
|
| 22 |
-
w_size = int((float(image.size[0]) * float(h_percent)))
|
| 23 |
-
image = image.resize((w_size, base_height), Image.LANCZOS)
|
| 24 |
-
|
| 25 |
-
# Convert the image to a byte stream
|
| 26 |
-
buffered = BytesIO()
|
| 27 |
-
image.save(buffered, format="JPEG")
|
| 28 |
-
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
print(f"Error: The file {image_path} was not found.")
|
| 33 |
-
return None
|
| 34 |
-
except Exception as e:
|
| 35 |
-
print(f"Error: {e}")
|
| 36 |
return None
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
def feifeichat(
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
"
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
}
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# Non-streaming call returns complete JSON payload directly to Gradio API
|
| 67 |
-
response = client.chat.completions.create(
|
| 68 |
-
model="qwen/qwen3.6-27b",
|
| 69 |
-
messages=messages,
|
| 70 |
-
response_format={"type": "json_object"},
|
| 71 |
-
reasoning_effort="none",
|
| 72 |
-
temperature=0.7,
|
| 73 |
-
stream=False
|
| 74 |
)
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
-
|
| 80 |
-
return f"An error occurred while processing your request: {e}"
|
| 81 |
|
| 82 |
with gr.Blocks() as demo:
|
| 83 |
-
gr.Markdown("Image
|
| 84 |
-
with gr.
|
| 85 |
-
input_img = gr.Image(
|
| 86 |
-
output_text = gr.Textbox(label="
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
submit_btn.click(
|
| 91 |
-
fn=feifeichat,
|
| 92 |
-
inputs=input_img,
|
| 93 |
-
outputs=output_text,
|
| 94 |
-
api_name="feifeichat"
|
| 95 |
-
)
|
| 96 |
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import io
|
| 3 |
+
import base64
|
| 4 |
+
import re
|
| 5 |
+
from PIL import Image
|
| 6 |
from groq import Groq
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
# Initialize Groq client
|
| 10 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def encode_image(image):
|
| 13 |
+
if image is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
completion = client.chat.completions.create(
|
| 38 |
+
model="llama-3.2-11b-vision-preview",
|
| 39 |
+
messages=[
|
| 40 |
+
{
|
| 41 |
+
"role": "user",
|
| 42 |
+
"content": [
|
| 43 |
+
{"type": "text", "text": prompt},
|
| 44 |
+
{
|
| 45 |
+
"type": "image_url",
|
| 46 |
+
"image_url": {
|
| 47 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
| 48 |
+
}
|
| 49 |
}
|
| 50 |
+
]
|
| 51 |
+
}
|
| 52 |
+
],
|
| 53 |
+
temperature=0.2,
|
| 54 |
+
max_tokens=1024
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
+
|
| 57 |
+
raw_response = completion.choices[0].message.content.strip()
|
| 58 |
+
|
| 59 |
+
# Strip thinking tags if present
|
| 60 |
+
cleaned_response = re.sub(r'<think>.*?</think>', '', raw_response, flags=re.DOTALL).strip()
|
| 61 |
+
# Strip Markdown code fencing block if present
|
| 62 |
+
cleaned_response = re.sub(r'^```(?:json)?\s*', '', cleaned_response)
|
| 63 |
+
cleaned_response = re.sub(r'\s*```$', '', cleaned_response)
|
| 64 |
+
|
| 65 |
+
return cleaned_response
|
| 66 |
except Exception as e:
|
| 67 |
+
return f'{{"error": "{str(e)}"}}'
|
|
|
|
| 68 |
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("# Image Captioning & Analysis API")
|
| 71 |
+
with gr.Row():
|
| 72 |
+
input_img = gr.Image(type="pil", label="Input Image")
|
| 73 |
+
output_text = gr.Textbox(label="JSON Output")
|
| 74 |
+
|
| 75 |
+
submit_btn = gr.Button("Analyze Image")
|
| 76 |
+
submit_btn.click(fn=feifeichat, inputs=input_img, outputs=output_text, api_name="feifeichat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|