proitm commited on
Commit
422d59d
·
verified ·
1 Parent(s): 2c459e0

Improved agent to work with files

Browse files
Files changed (2) hide show
  1. app.py +69 -6
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import gradio as gr
 
2
  import os
 
3
 
4
  from mcp import StdioServerParameters
5
  from smolagents import InferenceClientModel, CodeAgent, ToolCollection, MCPClient
@@ -30,18 +32,79 @@ tools = get_mcp_tools(MCP_SERVERS)
30
 
31
  model = InferenceClientModel(token=os.getenv("HF_TOKEN"))
32
  # @TODO: Change promot to ask for clarifications if necessary and set final answer.
33
- agent = CodeAgent(tools=[*tools], model=model, max_steps=8, planning_interval=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  chat = gr.ChatInterface(
36
- fn=lambda message, history: str(
37
- agent.run(
38
- "\n".join([str(x) for x in history[-10:]]) if history else "" + message
39
- )
40
- ),
41
  type="messages",
42
  examples=["Prime factorization of 68"],
43
  title="Agent with MCP Tools",
44
  description="This is a simple agent that uses MCP tools to answer questions.",
 
 
45
  save_history=True,
46
  )
47
 
 
1
  import gradio as gr
2
+ import json
3
  import os
4
+ import PyPDF2
5
 
6
  from mcp import StdioServerParameters
7
  from smolagents import InferenceClientModel, CodeAgent, ToolCollection, MCPClient
 
32
 
33
  model = InferenceClientModel(token=os.getenv("HF_TOKEN"))
34
  # @TODO: Change promot to ask for clarifications if necessary and set final answer.
35
+ agent = CodeAgent(
36
+ tools=[*tools],
37
+ model=model,
38
+ max_steps=8,
39
+ planning_interval=4,
40
+ stream_outputs=True,
41
+ )
42
+
43
+ memory = []
44
+
45
+ def format_message(memory, message):
46
+ return "History: \n" + "\n".join([str(x) for x in memory]) + "\nTask: " + message
47
+
48
+ def extract_text_from_txt(file_obj):
49
+ file_obj.seek(0)
50
+ content = file_obj.read()
51
+ if isinstance(content, bytes):
52
+ try:
53
+ content = content.decode("utf-8")
54
+ except Exception:
55
+ content = content.decode(errors="ignore")
56
+ return content
57
+
58
+ def extract_text_from_pdf(file_obj):
59
+ file_obj.seek(0)
60
+ try:
61
+ reader = PyPDF2.PdfReader(file_obj)
62
+ pdf_text = ""
63
+ for page in reader.pages:
64
+ pdf_text += page.extract_text() or ""
65
+ return pdf_text
66
+ except Exception as e:
67
+ return f"[Error reading PDF: {e}]"
68
+
69
+ def extract_text_from_file(file_obj):
70
+ filename = getattr(file_obj, "name")
71
+ if filename.lower().endswith(".txt"):
72
+ return {"name": filename, "text": extract_text_from_txt(file_obj)}
73
+ elif filename.lower().endswith(".pdf"):
74
+ return {"name": filename, "text": extract_text_from_pdf(file_obj)}
75
+ raise ValueError(f"Unsupported file type: {filename}")
76
+
77
+ def prepare_additional_args(message):
78
+ additional_args = {}
79
+ if "files" in message and message["files"]:
80
+ file_texts = [extract_text_from_file(open(file_obj, "rb")) for file_obj in message["files"]]
81
+ additional_args["uploaded_file_texts"] = file_texts
82
+ return additional_args
83
+
84
+ def chat_function(message, history):
85
+ # Add user message to memory
86
+ memory.append({"message": message["text"], "role": "user", "type": "text"})
87
+
88
+ # Prepare additional_args for the agent
89
+ additional_args = prepare_additional_args(message)
90
+
91
+ # Pass both the message and additional_args to the agent
92
+ reply = agent.run(
93
+ message["text"],
94
+ additional_args=additional_args
95
+ )
96
+
97
+ memory.append({"message": reply, "role": "assistant", "type": "text"})
98
+ return reply
99
 
100
  chat = gr.ChatInterface(
101
+ fn=chat_function,
 
 
 
 
102
  type="messages",
103
  examples=["Prime factorization of 68"],
104
  title="Agent with MCP Tools",
105
  description="This is a simple agent that uses MCP tools to answer questions.",
106
+ multimodal=True,
107
+ textbox=gr.MultimodalTextbox(file_types=[".pdf", ".txt"]),
108
  save_history=True,
109
  )
110
 
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  smolagents[mcp]
2
  gradio[mcp]
3
  mcp
4
- fastmcp
 
 
1
  smolagents[mcp]
2
  gradio[mcp]
3
  mcp
4
+ fastmcp
5
+ PyPDF2