Spaces:
Running
Running
Update app.py
Browse filesUse openai's websearch tool. PDF download working,
app.py
CHANGED
|
@@ -18,7 +18,7 @@ from urllib.parse import quote
|
|
| 18 |
from multiprocessing import context
|
| 19 |
# from agents.tool import WebSearchTool
|
| 20 |
from typing_extensions import TypedDict, Any
|
| 21 |
-
from agents import Agent, FunctionTool, ImageGenerationTool, RunContextWrapper, Runner, function_tool, CodeInterpreterTool, ModelSettings
|
| 22 |
from openai import OpenAI
|
| 23 |
from markdown_pdf import MarkdownPdf
|
| 24 |
from markdown_pdf import Section
|
|
@@ -60,7 +60,7 @@ async def make_pdf(text: str, title: str)->str:
|
|
| 60 |
title: A title that will be used as part of a filename in a file path.
|
| 61 |
|
| 62 |
Returns:
|
| 63 |
-
The
|
| 64 |
'''
|
| 65 |
for file in glob('./document.pdf'):
|
| 66 |
os.remove(file)
|
|
@@ -68,7 +68,7 @@ async def make_pdf(text: str, title: str)->str:
|
|
| 68 |
pdf.add_section(Section(text))
|
| 69 |
outpath = os.path.join('./','document.pdf')
|
| 70 |
pdf.save(outpath)
|
| 71 |
-
return
|
| 72 |
|
| 73 |
@function_tool
|
| 74 |
async def get_news(query: str, window: str)->str:
|
|
@@ -272,7 +272,7 @@ async def chat(prompt_window, user_window, password, history, output_window,
|
|
| 272 |
agent = Agent(name="Assistant",
|
| 273 |
instructions=instructions,
|
| 274 |
model_settings=ModelSettings(reasoning=Reasoning(effort='low', summary='detailed')),
|
| 275 |
-
tools=[get_distance,
|
| 276 |
CodeInterpreterTool(tool_config={"type": "code_interpreter","container": code_container}), # make_pdf,
|
| 277 |
ImageGenerationTool(tool_config={"type": "image_generation", "quality": "low"},)],)
|
| 278 |
|
|
@@ -344,6 +344,7 @@ async def chat(prompt_window, user_window, password, history, output_window,
|
|
| 344 |
exception_msg = ''
|
| 345 |
result = None
|
| 346 |
reasoning = ''
|
|
|
|
| 347 |
try:
|
| 348 |
result = await Runner.run(agent, max_turns=20,
|
| 349 |
input=inputs)
|
|
@@ -358,6 +359,22 @@ async def chat(prompt_window, user_window, password, history, output_window,
|
|
| 358 |
fp.write(base64.b64decode(img_result))
|
| 359 |
image_window = gr.Image(visible=True, value=image_out_path)
|
| 360 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
if item.type == "reasoning_item":
|
| 362 |
for o in item.raw_item.summary:
|
| 363 |
if o.type == "summary_text":
|
|
@@ -369,7 +386,11 @@ async def chat(prompt_window, user_window, password, history, output_window,
|
|
| 369 |
# for o in item.summary:
|
| 370 |
# reasoning += ('\nm1: ' + o.text + '\n')
|
| 371 |
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
response += "\n\n***YOU***: " + prompt + "\n\n***GPT***: " + reply.replace('```','\n\n```\n\n')
|
| 374 |
if reasoning != '':
|
| 375 |
response += '\n\n**Reasoning:**\n\n' + reasoning + '\n'
|
|
@@ -450,6 +471,7 @@ def show_help():
|
|
| 450 |
|
| 451 |
def new_session(user_window, history):
|
| 452 |
history = []
|
|
|
|
| 453 |
return [prompt_window, history, 'Session cleared',
|
| 454 |
gr.Image(visible=False, value=None),
|
| 455 |
gr.Image(visible=False, value=None), [],
|
|
|
|
| 18 |
from multiprocessing import context
|
| 19 |
# from agents.tool import WebSearchTool
|
| 20 |
from typing_extensions import TypedDict, Any
|
| 21 |
+
from agents import Agent, FunctionTool, ImageGenerationTool, RunContextWrapper, Runner, WebSearchTool, function_tool, CodeInterpreterTool, ModelSettings
|
| 22 |
from openai import OpenAI
|
| 23 |
from markdown_pdf import MarkdownPdf
|
| 24 |
from markdown_pdf import Section
|
|
|
|
| 60 |
title: A title that will be used as part of a filename in a file path.
|
| 61 |
|
| 62 |
Returns:
|
| 63 |
+
The title of the PDF that was created.
|
| 64 |
'''
|
| 65 |
for file in glob('./document.pdf'):
|
| 66 |
os.remove(file)
|
|
|
|
| 68 |
pdf.add_section(Section(text))
|
| 69 |
outpath = os.path.join('./','document.pdf')
|
| 70 |
pdf.save(outpath)
|
| 71 |
+
return title
|
| 72 |
|
| 73 |
@function_tool
|
| 74 |
async def get_news(query: str, window: str)->str:
|
|
|
|
| 272 |
agent = Agent(name="Assistant",
|
| 273 |
instructions=instructions,
|
| 274 |
model_settings=ModelSettings(reasoning=Reasoning(effort='low', summary='detailed')),
|
| 275 |
+
tools=[get_distance, WebSearchTool(), get_news, make_pdf, # search_web
|
| 276 |
CodeInterpreterTool(tool_config={"type": "code_interpreter","container": code_container}), # make_pdf,
|
| 277 |
ImageGenerationTool(tool_config={"type": "image_generation", "quality": "low"},)],)
|
| 278 |
|
|
|
|
| 344 |
exception_msg = ''
|
| 345 |
result = None
|
| 346 |
reasoning = ''
|
| 347 |
+
pdf_id = None
|
| 348 |
try:
|
| 349 |
result = await Runner.run(agent, max_turns=20,
|
| 350 |
input=inputs)
|
|
|
|
| 359 |
fp.write(base64.b64decode(img_result))
|
| 360 |
image_window = gr.Image(visible=True, value=image_out_path)
|
| 361 |
|
| 362 |
+
if item.type == 'tool_call_item':
|
| 363 |
+
raw = item.raw_item
|
| 364 |
+
if raw.name == 'make_pdf':
|
| 365 |
+
pdf_id = raw.call_id
|
| 366 |
+
|
| 367 |
+
if item.type == 'tool_call_output_item' and pdf_id:
|
| 368 |
+
raw_id = item.raw_item['call_id']
|
| 369 |
+
if raw_id == pdf_id:
|
| 370 |
+
# raw_data = item.raw_item['output']
|
| 371 |
+
# bin_out = base64.b64decode(raw_data)
|
| 372 |
+
# with open('./document.pdf', 'w', encoding='utf-8') as fp:
|
| 373 |
+
# fp.write(raw_data)
|
| 374 |
+
file_download = gr.DownloadButton(label=f'Download PDF Doc',
|
| 375 |
+
visible=True, value='./document.pdf')
|
| 376 |
+
|
| 377 |
+
|
| 378 |
if item.type == "reasoning_item":
|
| 379 |
for o in item.raw_item.summary:
|
| 380 |
if o.type == "summary_text":
|
|
|
|
| 386 |
# for o in item.summary:
|
| 387 |
# reasoning += ('\nm1: ' + o.text + '\n')
|
| 388 |
|
| 389 |
+
out_text = "\n".join(line for line in result.final_output.splitlines() if
|
| 390 |
+
not ('download' in line.casefold() and 'mnt' in line.casefold()))
|
| 391 |
+
if out_text == '':
|
| 392 |
+
out_text = 'Download using button below.'
|
| 393 |
+
reply = md(out_text)
|
| 394 |
response += "\n\n***YOU***: " + prompt + "\n\n***GPT***: " + reply.replace('```','\n\n```\n\n')
|
| 395 |
if reasoning != '':
|
| 396 |
response += '\n\n**Reasoning:**\n\n' + reasoning + '\n'
|
|
|
|
| 471 |
|
| 472 |
def new_session(user_window, history):
|
| 473 |
history = []
|
| 474 |
+
clean_up_files()
|
| 475 |
return [prompt_window, history, 'Session cleared',
|
| 476 |
gr.Image(visible=False, value=None),
|
| 477 |
gr.Image(visible=False, value=None), [],
|