zerovic commited on
Commit
1b7c1e0
·
verified ·
1 Parent(s): 40d8238

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -82
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
- # Read the key name from Hugging Face Environment Variables secrets configuration
9
- # Ensure "GROQ_API_KEY" is set in your Hugging Face Space settings.
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
- return img_str
31
- except FileNotFoundError:
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(image):
39
- try:
40
- if image is None:
41
- return "Please upload a photo"
 
 
 
 
 
 
42
 
43
- base64_image = encode_image(image)
44
- if not base64_image:
45
- return "Error processing image"
 
 
46
 
47
- # Engineered prompt utilizing explicit style rules, forbidden terms, and keyword density targets
48
- messages = [
49
- {
50
- "role": "user",
51
- "content": [
52
- {
53
- "type": "text",
54
- "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'])."
55
- },
56
- {
57
- "type": "image_url",
58
- "image_url": {
59
- "url": f"data:image/jpeg;base64,{base64_image}"
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
- return response.choices[0].message.content
77
-
 
 
 
 
 
 
 
78
  except Exception as e:
79
- print(f"Error: {e}")
80
- return f"An error occurred while processing your request: {e}"
81
 
82
  with gr.Blocks() as demo:
83
- gr.Markdown("Image To Flux Prompt")
84
- with gr.Tab(label="Image To Flux Prompt"):
85
- input_img = gr.Image(label="Input Picture", height=320, type="filepath")
86
- output_text = gr.Textbox(label="Flux Prompt")
87
- submit_btn = gr.Button(value="Submit")
88
-
89
- # api_name="feifeichat" explicitly exposes /gradio_api/call/feifeichat
90
- submit_btn.click(
91
- fn=feifeichat,
92
- inputs=input_img,
93
- outputs=output_text,
94
- api_name="feifeichat"
95
- )
96
 
97
- demo.launch()
 
 
 
 
 
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()