wjnwjn59 commited on
Commit
0f47a3e
Β·
1 Parent(s): b36a835

update temperature

Browse files
Files changed (1) hide show
  1. app.py +36 -21
app.py CHANGED
@@ -1,49 +1,61 @@
1
  import os, base64, json, uuid, torch, gradio as gr
2
  from pathlib import Path
3
- from src.llm.chat import FunctionCallingChat
4
 
5
- chatbot = FunctionCallingChat()
 
6
 
7
  def image_to_base64(image_path: str):
8
  with open(image_path, "rb") as f:
9
  return base64.b64encode(f.read()).decode("utf-8")
10
 
11
  def save_uploaded_image(pil_img) -> Path:
 
12
  Path("static").mkdir(exist_ok=True)
13
  filename = f"upload_{uuid.uuid4().hex[:8]}.png"
14
  path = Path("static") / filename
15
  pil_img.save(path)
16
  return path
17
 
18
- def inference(pil_img, prompt, task):
19
  if pil_img is None:
20
  return "❗ Please upload an image first."
21
 
22
- img_path = save_uploaded_image(pil_img)
 
23
 
 
24
  if task == "Detection":
25
  user_msg = f"Please detect objects in the image '{img_path}'."
26
  elif task == "Segmentation":
27
  user_msg = f"Please segment objects in the image '{img_path}'."
28
- else:
29
  prompt = prompt.strip() or "Analyse this image."
30
  user_msg = f"{prompt} (image: '{img_path}')"
31
 
32
- out = chatbot(user_msg)
33
- txt = (
34
- "### πŸ”§ Raw tool-call \n"
35
- f"{out['raw_tool_call']}\n\n"
36
- "### πŸ“¦ Tool results\n"
37
- f"{json.dumps(out['results'], indent=2)}"
38
- )
39
- return txt
40
-
 
 
 
 
 
 
 
 
41
  def create_header():
42
  with gr.Row():
43
  with gr.Column(scale=1):
44
  logo_base64 = image_to_base64("static/aivn_logo.png")
45
  gr.HTML(
46
- f"""<img src="data:image/png;base64,{logo_base64}"
47
  alt="Logo"
48
  style="height:120px;width:auto;margin-right:20px;margin-bottom:20px;">"""
49
  )
@@ -63,7 +75,6 @@ def create_header():
63
  """
64
  )
65
 
66
-
67
  def create_footer():
68
  footer_html = """
69
  <style>
@@ -80,7 +91,6 @@ def create_footer():
80
  """
81
  return gr.HTML(footer_html)
82
 
83
-
84
  custom_css = """
85
  .gradio-container {min-height:100vh;}
86
  .content-wrap {padding-bottom:60px;}
@@ -90,6 +100,7 @@ custom_css = """
90
  .full-width-btn:hover {background:linear-gradient(45deg,#FF5252,#3CB4AC)!important;}
91
  """
92
 
 
93
  with gr.Blocks(css=custom_css) as demo:
94
  create_header()
95
 
@@ -97,9 +108,13 @@ with gr.Blocks(css=custom_css) as demo:
97
  with gr.Column(scale=3):
98
  upload_image = gr.Image(label="Upload image", type="pil")
99
  prompt_input = gr.Textbox(label="Optional prompt", placeholder="e.g. Detect cats only")
100
- task_choice = gr.Radio(
101
- ["Auto", "Detection", "Segmentation"], value="Auto", label="Task"
102
- )
 
 
 
 
103
  submit_btn = gr.Button("Run πŸ”§", elem_classes="full-width-btn")
104
 
105
  with gr.Column(scale=4):
@@ -107,7 +122,7 @@ with gr.Blocks(css=custom_css) as demo:
107
 
108
  submit_btn.click(
109
  inference,
110
- inputs=[upload_image, prompt_input, task_choice],
111
  outputs=output_text,
112
  )
113
 
 
1
  import os, base64, json, uuid, torch, gradio as gr
2
  from pathlib import Path
3
+ from src.llm.chat import FunctionCallingChat
4
 
5
+ chatbot = FunctionCallingChat()
6
+ chatbot.temperature = 0.7
7
 
8
  def image_to_base64(image_path: str):
9
  with open(image_path, "rb") as f:
10
  return base64.b64encode(f.read()).decode("utf-8")
11
 
12
  def save_uploaded_image(pil_img) -> Path:
13
+ """Save PIL image to ./static and return its path."""
14
  Path("static").mkdir(exist_ok=True)
15
  filename = f"upload_{uuid.uuid4().hex[:8]}.png"
16
  path = Path("static") / filename
17
  pil_img.save(path)
18
  return path
19
 
20
+ def inference(pil_img, prompt, task, temperature):
21
  if pil_img is None:
22
  return "❗ Please upload an image first."
23
 
24
+ img_path = save_uploaded_image(pil_img)
25
+ chatbot.temperature = temperature
26
 
27
+ # build user message
28
  if task == "Detection":
29
  user_msg = f"Please detect objects in the image '{img_path}'."
30
  elif task == "Segmentation":
31
  user_msg = f"Please segment objects in the image '{img_path}'."
32
+ else:
33
  prompt = prompt.strip() or "Analyse this image."
34
  user_msg = f"{prompt} (image: '{img_path}')"
35
 
36
+ try:
37
+ out = chatbot(user_msg)
38
+ txt = (
39
+ "### πŸ”§ Raw tool-call\n"
40
+ f"{out['raw_tool_call']}\n\n"
41
+ "### πŸ“¦ Tool results\n"
42
+ f"{json.dumps(out['results'], indent=2)}"
43
+ )
44
+ return txt
45
+ finally:
46
+ # 4️⃣ always delete the temp image
47
+ try:
48
+ img_path.unlink(missing_ok=True)
49
+ except Exception:
50
+ pass # if deletion fails we just move on
51
+
52
+ # ──────────────────────────── UI ────────────────────────────
53
  def create_header():
54
  with gr.Row():
55
  with gr.Column(scale=1):
56
  logo_base64 = image_to_base64("static/aivn_logo.png")
57
  gr.HTML(
58
+ f"""<img src="data:image/png;base64,{logo_base64}"
59
  alt="Logo"
60
  style="height:120px;width:auto;margin-right:20px;margin-bottom:20px;">"""
61
  )
 
75
  """
76
  )
77
 
 
78
  def create_footer():
79
  footer_html = """
80
  <style>
 
91
  """
92
  return gr.HTML(footer_html)
93
 
 
94
  custom_css = """
95
  .gradio-container {min-height:100vh;}
96
  .content-wrap {padding-bottom:60px;}
 
100
  .full-width-btn:hover {background:linear-gradient(45deg,#FF5252,#3CB4AC)!important;}
101
  """
102
 
103
+ # ──────────────────────────── Blocks ─────────────────────────
104
  with gr.Blocks(css=custom_css) as demo:
105
  create_header()
106
 
 
108
  with gr.Column(scale=3):
109
  upload_image = gr.Image(label="Upload image", type="pil")
110
  prompt_input = gr.Textbox(label="Optional prompt", placeholder="e.g. Detect cats only")
111
+ task_choice = gr.Radio(["Auto", "Detection", "Segmentation"],
112
+ value="Auto", label="Task")
113
+
114
+ # NEW temperature slider
115
+ temp_slider = gr.Slider(minimum=0.1, maximum=1.5, step=0.1,
116
+ value=0.7, label="Temperature (sampling)")
117
+
118
  submit_btn = gr.Button("Run πŸ”§", elem_classes="full-width-btn")
119
 
120
  with gr.Column(scale=4):
 
122
 
123
  submit_btn.click(
124
  inference,
125
+ inputs=[upload_image, prompt_input, task_choice, temp_slider],
126
  outputs=output_text,
127
  )
128