AI-Quotient commited on
Commit
0d10f9a
·
verified ·
1 Parent(s): 4b03347

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -5
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import os
3
-
4
  os.environ["OPENAI_API_KEY"] = os.getenv('api_key')
5
 
6
  import math
@@ -101,7 +101,7 @@ builder = create_agent(llm, tool_registry)
101
  agent = builder.compile(store=store)
102
 
103
  def pvsnp(problem):
104
- output = []
105
  for step in agent.stream(
106
  {"messages": "Use tools to answer:"+problem},
107
  stream_mode="updates",
@@ -110,14 +110,37 @@ def pvsnp(problem):
110
  for message in update.get("messages", []):
111
  message.pretty_print()
112
  output.append(message.pretty_print())
113
- print (output)
114
- return output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
 
117
  iface = gr.Interface(
118
  fn=pvsnp,
119
  inputs=gr.Textbox(label="What problem would you like to classify as P or NP?"),
120
- outputs=gr.Markdown(label="Agent's response"), # Output as HTML
121
  title="PolyMath",
122
  description="PolyMath is an advanced AI agent that guides users through the intricate maze of computational complexity. This agent scrutinizes problem descriptions with sophisticated LLM prompts and symbolic reasoning. It classifies problems into categories such as P, NP, NP-complete, NP-hard, or beyond (e.g., PSPACE, EXPTIME), while providing clear, concise explanations of its reasoning. As part of AI Quotient’s Millennium Math Challenge, it is the first step towards solving the P vs NP problem.",
123
  theme = gr.themes.Ocean(),
 
1
  import gradio as gr
2
  import os
3
+ import json
4
  os.environ["OPENAI_API_KEY"] = os.getenv('api_key')
5
 
6
  import math
 
101
  agent = builder.compile(store=store)
102
 
103
  def pvsnp(problem):
104
+ '''output = []
105
  for step in agent.stream(
106
  {"messages": "Use tools to answer:"+problem},
107
  stream_mode="updates",
 
110
  for message in update.get("messages", []):
111
  message.pretty_print()
112
  output.append(message.pretty_print())
113
+ print (output)'''
114
+ output = agent.invoke({"messages": "Use tools to answer: "+problem})
115
+
116
+ # Assuming `output` is a JSON-formatted string
117
+ data = json.loads(output)
118
+
119
+ # Extract the answer from the final AIMessage
120
+ messages = data.get("messages", [])
121
+ answer = None
122
+ for msg in reversed(messages):
123
+ if msg.get("content"):
124
+ answer = msg["content"]
125
+ break
126
+
127
+ # Extract tool names used in tool_calls
128
+ tools_used = []
129
+ for msg in messages:
130
+ if "tool_calls" in msg:
131
+ for tool_call in msg["tool_calls"]:
132
+ tools_used.append(tool_call["name"])
133
+
134
+ print("Answer:", answer)
135
+ print("Tools used:", tools_used)
136
+ response = "Answer: "+answer+'\n'+"Tools used: "+tools_used
137
+ return response
138
 
139
 
140
  iface = gr.Interface(
141
  fn=pvsnp,
142
  inputs=gr.Textbox(label="What problem would you like to classify as P or NP?"),
143
+ outputs=gr.Textbox(label="Agent's response"), # Output as HTML
144
  title="PolyMath",
145
  description="PolyMath is an advanced AI agent that guides users through the intricate maze of computational complexity. This agent scrutinizes problem descriptions with sophisticated LLM prompts and symbolic reasoning. It classifies problems into categories such as P, NP, NP-complete, NP-hard, or beyond (e.g., PSPACE, EXPTIME), while providing clear, concise explanations of its reasoning. As part of AI Quotient’s Millennium Math Challenge, it is the first step towards solving the P vs NP problem.",
146
  theme = gr.themes.Ocean(),