luke9705 commited on
Commit
c28df4a
·
1 Parent(s): bb09fb2

Update system prompt and enhance image generation response handling

Browse files
Files changed (2) hide show
  1. app.py +9 -5
  2. system_prompt.txt +14 -2
app.py CHANGED
@@ -13,6 +13,7 @@ import openai
13
  from openai import OpenAI
14
  import pdfplumber
15
 
 
16
  ## utilty functions
17
  def is_image_extension(filename: str) -> bool:
18
  IMAGE_EXTS = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', '.svg'}
@@ -45,7 +46,7 @@ def load_file(path: str) -> list | dict:
45
  return {"raw document text": text, "audio path": path}
46
  else:
47
  return {"raw document text": text, "file path": path}
48
-
49
 
50
  ## tools definition
51
  @tool
@@ -126,8 +127,8 @@ def generate_image(prompt: str, neg_prompt: str) -> Image.Image:
126
  image_data = base64.b64decode(completion.to_dict()['data'][0]['b64_json'])
127
  image = BytesIO(image_data)
128
  image = Image.open(image).convert("RGB")
129
- return gr.Image(value=image)
130
 
 
131
 
132
 
133
  ## agent definition
@@ -145,15 +146,16 @@ class Agent:
145
  system_prompt = f.read()
146
  self.agent.prompt_templates["system_prompt"] = system_prompt
147
 
148
- print("System prompt:", self.agent.prompt_templates["system_prompt"])
149
 
150
  def __call__(self, message: str, images: Optional[list[Image.Image]] = None, files: Optional[str] = None) -> str:
151
  answer = self.agent.run(message, images = images, additional_args={"files": files})
152
  return answer
153
 
154
  ## gradio functions
155
- def respond(message, history):
156
 
 
157
  text = message.get("text", "")
158
  if not message.get("files"): # no files uploaded
159
  print("No files received.")
@@ -168,10 +170,12 @@ def respond(message, history):
168
  file = load_file(files[0])
169
  message = agent(text, files=file)
170
 
 
 
 
171
  return message
172
 
173
 
174
-
175
  def initialize_agent():
176
  agent = Agent()
177
  print("Agent initialized.")
 
13
  from openai import OpenAI
14
  import pdfplumber
15
 
16
+
17
  ## utilty functions
18
  def is_image_extension(filename: str) -> bool:
19
  IMAGE_EXTS = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', '.svg'}
 
46
  return {"raw document text": text, "audio path": path}
47
  else:
48
  return {"raw document text": text, "file path": path}
49
+
50
 
51
  ## tools definition
52
  @tool
 
127
  image_data = base64.b64decode(completion.to_dict()['data'][0]['b64_json'])
128
  image = BytesIO(image_data)
129
  image = Image.open(image).convert("RGB")
 
130
 
131
+ return gr.Image(value=image, label="Generated Image")
132
 
133
 
134
  ## agent definition
 
146
  system_prompt = f.read()
147
  self.agent.prompt_templates["system_prompt"] = system_prompt
148
 
149
+ #print("System prompt:", self.agent.prompt_templates["system_prompt"])
150
 
151
  def __call__(self, message: str, images: Optional[list[Image.Image]] = None, files: Optional[str] = None) -> str:
152
  answer = self.agent.run(message, images = images, additional_args={"files": files})
153
  return answer
154
 
155
  ## gradio functions
156
+ def respond(message: str, history : dict):
157
 
158
+ # input
159
  text = message.get("text", "")
160
  if not message.get("files"): # no files uploaded
161
  print("No files received.")
 
170
  file = load_file(files[0])
171
  message = agent(text, files=file)
172
 
173
+ # output
174
+ print("Agent response:", message)
175
+
176
  return message
177
 
178
 
 
179
  def initialize_agent():
180
  agent = Agent()
181
  print("Agent initialized.")
system_prompt.txt CHANGED
@@ -237,8 +237,7 @@ derail you from the true variables.
237
  9. The state persists between code executions: so if in one step you've created
238
  variables or imported modules, these will all persist.
239
  10. Don't give up! You're in charge of solving the task, not providing directions to
240
- solve it.
241
-
242
 
243
  ----
244
 
@@ -314,6 +313,19 @@ Additional domain-specific behaviors:
314
  - Where applicable, the assistant must provide annotated examples and suggest possible reframing or source guidance (e.g., UNESCO media diversity principles).
315
  ----
316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
  Now Begin!
318
 
319
 
 
237
  9. The state persists between code executions: so if in one step you've created
238
  variables or imported modules, these will all persist.
239
  10. Don't give up! You're in charge of solving the task, not providing directions to
240
+ solve it.
 
241
 
242
  ----
243
 
 
313
  - Where applicable, the assistant must provide annotated examples and suggest possible reframing or source guidance (e.g., UNESCO media diversity principles).
314
  ----
315
 
316
+
317
+ It is MANDATORY to use these rules for the 'final_answer' tool:
318
+ 1. Always return a Python list. Do not return a dictionary or any other type.
319
+ 2. The first element of the list must be a Gradio component (for example, gr.Image(…), gr.File(…), etc.).
320
+ 3. Any text or explanation must come after the component, as string elements in the same list.
321
+ 4. If there is no component to return, return a list whose only element is the text.
322
+ 5. Examples of valid returns:
323
+ • [gr.Image(value=“output.png”), “Here is the chart.”]
324
+ • [None, “No media available, here is the text.”]
325
+ • [gr.File(value=“report.pdf”), “Download the report.”]
326
+ 7. Any deviation (returning a dict, tuple, raw PIL image, etc.) is invalid.
327
+
328
+
329
  Now Begin!
330
 
331