kfkas commited on
Commit
f6f4388
ยท
1 Parent(s): 862e4a8
Files changed (1) hide show
  1. app.py +61 -41
app.py CHANGED
@@ -1,6 +1,8 @@
 
1
  import os
2
  import shutil
3
  import time
 
4
 
5
  import cv2
6
  import base64
@@ -238,53 +240,71 @@ class TipCalculator:
238
  self.model_clients = model_clients
239
  self.video_processor = video_processor
240
 
241
- def parse_llm_output(self, output_text):
242
- """LLM ์ถœ๋ ฅ์„ ํŒŒ์‹ฑํ•˜์—ฌ ํŒ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ ์ถ”์ถœ"""
243
  analysis = "Analysis not found."
244
  tip_percentage = 0.0
245
  tip_amount = 0.0
246
  total_bill = 0.0
247
 
248
- # Analysis ๋ถ€๋ถ„: "Analysis:" ์ดํ›„๋ถ€ํ„ฐ "**Final Tip Percentage**" ์ด์ „๊นŒ์ง€ ์ถ”์ถœ
249
- analysis_match = re.search(r"Analysis:\s*(.*?)\*\*Final Tip Percentage\*\*", output_text,
250
- re.DOTALL | re.IGNORECASE)
251
- if analysis_match:
252
- analysis = analysis_match.group(1).strip()
253
- else:
254
- analysis_match_alt = re.search(r"Analysis:\s*(.*)", output_text, re.DOTALL | re.IGNORECASE)
255
- if analysis_match_alt:
256
- analysis = analysis_match_alt.group(1).strip()
257
-
258
- # **Final Tip Percentage** ์ถ”์ถœ (์˜ˆ: **Final Tip Percentage**: 2.00%)
259
- percentage_match = re.search(r"\*\*Final Tip Percentage\*\*:\s*([0-9]+(?:\.[0-9]+)?)%", output_text,
260
- re.DOTALL | re.IGNORECASE)
261
- if percentage_match:
262
- try:
263
- tip_percentage = float(percentage_match.group(1))
264
- except ValueError:
265
- print(f"๊ฒฝ๊ณ : Tip Percentage ๋ณ€ํ™˜ ์‹คํŒจ - {percentage_match.group(1)}")
266
- tip_percentage = 0.0
267
-
268
- # **Final Tip Amount** ์ถ”์ถœ (์˜ˆ: **Final Tip Amount**: $1.44)
269
- tip_match = re.search(r"\*\*Final Tip Amount\*\*:\s*\$?\s*([0-9]+(?:\.[0-9]+)?)", output_text, re.IGNORECASE)
270
- if tip_match:
271
  try:
272
- tip_amount = float(tip_match.group(1))
273
- except ValueError:
274
- print(f"๊ฒฝ๊ณ : Tip Amount ๋ณ€ํ™˜ ์‹คํŒจ - {tip_match.group(1)}")
275
- tip_amount = 0.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  else:
277
- print(f"๊ฒฝ๊ณ : ์ถœ๋ ฅ์—์„œ Tip Amount๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค:\n{output_text}")
278
 
279
- # **Final Total Bill** ์ถ”์ถœ (์˜ˆ: **Final Total Bill**: $73.44)
280
- total_match = re.search(r"\*\*Final Total Bill\*\*:\s*\$?\s*([0-9]+(?:\.[0-9]+)?)", output_text, re.IGNORECASE)
281
- if total_match:
282
- try:
283
- total_bill = float(total_match.group(1))
284
- except ValueError:
285
- print(f"๊ฒฝ๊ณ : Total Bill ๋ณ€ํ™˜ ์‹คํŒจ - {total_match.group(1)}")
286
- if len(analysis) < 20 and analysis == "Analysis not found.":
287
- analysis = output_text
288
 
289
  return analysis, tip_percentage, tip_amount, output_text
290
 
@@ -673,8 +693,8 @@ class App:
673
  gr.Examples(
674
  examples=[
675
  # ์ž…๋ ฅ ์ˆœ์„œ: [Alibaba API Key, Video, Subtotal, Star Rating, Review] + [๊ฐ ์Œ์‹์˜ Qty]
676
- ["video/sample.mp4", 0.0, 1, "He drop the tray..so bad", 0, 0, 0, 0, 0, 2, 0, 0],
677
- ["video/sample2.mp4", 0.0, 5, "Good service!", 0, 0, 0, 0, 0, 2, 0, 0]
678
  ],
679
  inputs=[video_input, subtotal_display, rating_input,
680
  review_input] + quantity_inputs,
 
1
+ import json
2
  import os
3
  import shutil
4
  import time
5
+ from typing import Tuple
6
 
7
  import cv2
8
  import base64
 
240
  self.model_clients = model_clients
241
  self.video_processor = video_processor
242
 
243
+ def parse_llm_output(self, output_text: str) -> Tuple[str, float, float, float, str]:
 
244
  analysis = "Analysis not found."
245
  tip_percentage = 0.0
246
  tip_amount = 0.0
247
  total_bill = 0.0
248
 
249
+ # ------------------------------------------------------------------
250
+ # 1) JSON ๋ธ”๋ก ํƒ์ƒ‰ ```json ... ``` ๋˜๋Š” { "final_tip_percentage": ... }
251
+ # ------------------------------------------------------------------
252
+ json_block = None
253
+
254
+ # 1โ€‘A) ```json ... ``` ํŒจํ„ด
255
+ m = re.search(r"```json\s*(\{.*?\})\s*```", output_text, re.DOTALL | re.IGNORECASE)
256
+ if not m:
257
+ # 1โ€‘B) ๋ฐฑํ‹ฑ์ด ์—†๋Š” ๊ฒฝ์šฐ, ์ฒซ ๋ฒˆ์งธ { ... } ์ค‘์— key ๊ฐ€ ํฌํ•จ๋ผ ์žˆ์œผ๋ฉด ์žก๊ธฐ
258
+ m = re.search(r"(\{[^{}]*\"final_tip_percentage\"[^{}]*\})", output_text,
259
+ re.DOTALL | re.IGNORECASE)
260
+
261
+ if m:
262
+ json_block = m.group(1)
 
 
 
 
 
 
 
 
 
263
  try:
264
+ json_data = json.loads(json_block)
265
+ tip_percentage = float(json_data.get("final_tip_percentage", 0.0))
266
+ tip_amount = float(json_data.get("final_tip_amount", 0.0))
267
+ total_bill = float(json_data.get("final_total_bill", 0.0))
268
+ except (ValueError, json.JSONDecodeError) as e:
269
+ print(f"[WARN] JSON ํŒŒ์‹ฑ ์‹คํŒจ: {e}")
270
+
271
+ # ------------------------------------------------------------------
272
+ # 2) JSON์ด ์—†์œผ๋ฉด ๊ตฌ(่ˆŠ) ํฌ๋งท(**Final Tip โ€ฆ**)์œผ๋กœ ํŒŒ์‹ฑ
273
+ # ------------------------------------------------------------------
274
+ if json_block is None:
275
+ # Tip %
276
+ m = re.search(r"\*\*Final Tip Percentage\*\*:\s*([0-9]+(?:\.[0-9]+)?)%", output_text,
277
+ re.IGNORECASE)
278
+ if m:
279
+ tip_percentage = float(m.group(1))
280
+
281
+ # Tip Amount
282
+ m = re.search(r"\*\*Final Tip Amount\*\*:\s*\$?\s*([0-9]+(?:\.[0-9]+)?)", output_text,
283
+ re.IGNORECASE)
284
+ if m:
285
+ tip_amount = float(m.group(1))
286
+
287
+ # Total Bill
288
+ m = re.search(r"\*\*Final Total Bill\*\*:\s*\$?\s*([0-9]+(?:\.[0-9]+)?)", output_text,
289
+ re.IGNORECASE)
290
+ if m:
291
+ total_bill = float(m.group(1))
292
+
293
+ # ------------------------------------------------------------------
294
+ # 3) Analysis ์ถ”์ถœ (JSON ๋ธ”๋ก ์•ž๋ถ€๋ถ„์„ ๊ทธ๋Œ€๋กœ ์“ฐ๊ฑฐ๋‚˜, ํ‚ค์›Œ๋“œ๋กœ ํŒŒ์‹ฑ)
295
+ # ------------------------------------------------------------------
296
+ if json_block:
297
+ analysis_part = output_text.split(json_block)[0] # JSON ์•ž ํ…์ŠคํŠธ
298
  else:
299
+ analysis_part = output_text
300
 
301
+ # โ€œAnalysis:โ€ ๋˜๋Š” โ€œOverall Analysis:โ€ ์ดํ›„๋งŒ ์ž˜๋ผ ์ฃผ๋ฉด ๋” ๊น”๋”
302
+ m = re.search(r"(?:Overall\s+Analysis|Analysis):\s*(.*)", analysis_part,
303
+ re.DOTALL | re.IGNORECASE)
304
+ if m:
305
+ analysis = m.group(1).strip()
306
+ else:
307
+ analysis = analysis_part.strip()
 
 
308
 
309
  return analysis, tip_percentage, tip_amount, output_text
310
 
 
693
  gr.Examples(
694
  examples=[
695
  # ์ž…๋ ฅ ์ˆœ์„œ: [Alibaba API Key, Video, Subtotal, Star Rating, Review] + [๊ฐ ์Œ์‹์˜ Qty]
696
+ ["./video/sample.mp4", 0.0, 1, "He drop the tray..so bad", 0, 0, 0, 0, 0, 2, 0, 0],
697
+ ["./video/sample2.mp4", 0.0, 5, "Good service!", 0, 0, 0, 0, 0, 2, 0, 0]
698
  ],
699
  inputs=[video_input, subtotal_display, rating_input,
700
  review_input] + quantity_inputs,