Fix errors
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
|
|
|
| 3 |
from contextlib import suppress
|
| 4 |
from io import BytesIO
|
| 5 |
from pprint import pprint
|
|
@@ -31,6 +32,8 @@ from langchain_tavily import TavilySearch
|
|
| 31 |
# --- Constants ---
|
| 32 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 33 |
|
|
|
|
|
|
|
| 34 |
|
| 35 |
class State(MessagesState):
|
| 36 |
question: str
|
|
@@ -48,8 +51,7 @@ class BasicAgent:
|
|
| 48 |
]
|
| 49 |
|
| 50 |
# Chat model with tool support
|
| 51 |
-
self.
|
| 52 |
-
self.model_with_tools = self.model.bind_tools(self.tools, parallel_tool_calls=False)
|
| 53 |
|
| 54 |
# LangGraph
|
| 55 |
self.graph = StateGraph(State)
|
|
@@ -72,7 +74,8 @@ class BasicAgent:
|
|
| 72 |
1. **Thought:** Briefly outline your reasoning step.
|
| 73 |
2. **Reflect:** Check “Did I use all observations? Did my tool call succeed?”
|
| 74 |
3. **Action:** Either call a tool (with arguments) or prepare your final answer.
|
| 75 |
-
4. **Final Answer:** Provide only the bare result (no labels, no extra text).
|
|
|
|
| 76 |
|
| 77 |
**Answer Format Rules**
|
| 78 |
- If the answer is a number, output digits only (no commas, no units, no strings like “one”, “twenty three”).
|
|
@@ -109,7 +112,7 @@ class BasicAgent:
|
|
| 109 |
question=question,
|
| 110 |
messages=[sys_msg, HumanMessage(content=question)]
|
| 111 |
)
|
| 112 |
-
config = RunnableConfig(recursion_limit=
|
| 113 |
result = self.compiled_graph.invoke(state, config)
|
| 114 |
final_answer = result["messages"][-1].content
|
| 115 |
print(f"\nFinal Answer: {final_answer}")
|
|
@@ -175,13 +178,23 @@ class BasicAgent:
|
|
| 175 |
return f"Error: file not found at {path}"
|
| 176 |
|
| 177 |
print("File metadata:", os.stat(path))
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
img_bytes.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
vision = ChatOpenAI(model="gpt-4o-vision", temperature=0)
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
return result
|
| 186 |
|
| 187 |
@staticmethod
|
|
@@ -216,7 +229,7 @@ class BasicAgent:
|
|
| 216 |
|
| 217 |
Please answer briefly based on this transcript, and give only the answer."""
|
| 218 |
|
| 219 |
-
response =
|
| 220 |
answer = response.choices[0].text.strip()
|
| 221 |
|
| 222 |
return answer[:max_chars]
|
|
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
+
from base64 import b64encode
|
| 4 |
from contextlib import suppress
|
| 5 |
from io import BytesIO
|
| 6 |
from pprint import pprint
|
|
|
|
| 32 |
# --- Constants ---
|
| 33 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 34 |
|
| 35 |
+
model = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 36 |
+
|
| 37 |
|
| 38 |
class State(MessagesState):
|
| 39 |
question: str
|
|
|
|
| 51 |
]
|
| 52 |
|
| 53 |
# Chat model with tool support
|
| 54 |
+
self.model_with_tools = model.bind_tools(self.tools, parallel_tool_calls=False)
|
|
|
|
| 55 |
|
| 56 |
# LangGraph
|
| 57 |
self.graph = StateGraph(State)
|
|
|
|
| 74 |
1. **Thought:** Briefly outline your reasoning step.
|
| 75 |
2. **Reflect:** Check “Did I use all observations? Did my tool call succeed?”
|
| 76 |
3. **Action:** Either call a tool (with arguments) or prepare your final answer.
|
| 77 |
+
4. **Final Answer:** Provide only the bare result (no labels, no extra text, no thoughts, no reflection, no "Final Answer" string in the result). For question that contain phrases like `what is the number` or
|
| 78 |
+
`what is the highest number` return just the number, e.g., 2.
|
| 79 |
|
| 80 |
**Answer Format Rules**
|
| 81 |
- If the answer is a number, output digits only (no commas, no units, no strings like “one”, “twenty three”).
|
|
|
|
| 112 |
question=question,
|
| 113 |
messages=[sys_msg, HumanMessage(content=question)]
|
| 114 |
)
|
| 115 |
+
config = RunnableConfig(recursion_limit=15)
|
| 116 |
result = self.compiled_graph.invoke(state, config)
|
| 117 |
final_answer = result["messages"][-1].content
|
| 118 |
print(f"\nFinal Answer: {final_answer}")
|
|
|
|
| 178 |
return f"Error: file not found at {path}"
|
| 179 |
|
| 180 |
print("File metadata:", os.stat(path))
|
| 181 |
+
|
| 182 |
+
# img = Image.open(path)
|
| 183 |
+
# img_bytes = BytesIO()
|
| 184 |
+
# img.save(img_bytes, format=img.format)
|
| 185 |
+
# img_bytes.seek(0)
|
| 186 |
+
|
| 187 |
+
with open("photo.png","rb") as f:
|
| 188 |
+
b64 = b64encode(f.read()).decode()
|
| 189 |
|
| 190 |
vision = ChatOpenAI(model="gpt-4o-vision", temperature=0)
|
| 191 |
+
msg = HumanMessage(content=[
|
| 192 |
+
{"type":"text", "text": question},
|
| 193 |
+
{"type":"image_base64", "image_base64": {"data": b64}}
|
| 194 |
+
])
|
| 195 |
+
response = vision.invoke([SystemMessage(content="Analyze the image."), msg])
|
| 196 |
+
result = response.content
|
| 197 |
+
print("Result:", result)
|
| 198 |
return result
|
| 199 |
|
| 200 |
@staticmethod
|
|
|
|
| 229 |
|
| 230 |
Please answer briefly based on this transcript, and give only the answer."""
|
| 231 |
|
| 232 |
+
response = model(completion_kwargs={"max_tokens": 200})(prompt)
|
| 233 |
answer = response.choices[0].text.strip()
|
| 234 |
|
| 235 |
return answer[:max_chars]
|