zerovic commited on
Commit
151b1e3
·
verified ·
1 Parent(s): 1b7c1e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -65
app.py CHANGED
@@ -1,79 +1,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()
 
 
 
 
 
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()