philincloud commited on
Commit
9f35218
·
verified ·
1 Parent(s): 5947d1e

Update langgraph_agent.py

Browse files
Files changed (1) hide show
  1. langgraph_agent.py +48 -4
langgraph_agent.py CHANGED
@@ -3,6 +3,7 @@ import io
3
  import contextlib
4
  import pandas as pd
5
  from typing import Dict, List, Union
 
6
 
7
  from PIL import Image as PILImage
8
  from huggingface_hub import InferenceClient
@@ -159,10 +160,43 @@ def transcribe_audio(audio_path: str) -> Dict[str, str]:
159
  except Exception as e:
160
  return {"error": f"Error transcribing audio {audio_path}: {str(e)}"}
161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  API_KEY = os.getenv("GEMINI_API_KEY")
163
- HF_API_TOKEN = os.getenv("HF_SPACE_TOKEN")
164
- GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
165
 
 
166
  tools = [
167
  multiply, add, subtract, divide, modulus,
168
  wiki_search,
@@ -172,6 +206,7 @@ tools = [
172
  python_interpreter,
173
  describe_image,
174
  transcribe_audio,
 
175
  ]
176
 
177
  with open("prompt.txt", "r", encoding="utf-8") as f:
@@ -197,17 +232,18 @@ def build_graph(provider: str = "gemini"):
197
  else:
198
  raise ValueError("Invalid provider. Choose 'gemini' or 'huggingface'.")
199
 
 
200
  llm_with_tools = llm.bind_tools(tools)
201
 
202
  def assistant(state: MessagesState):
203
  messages_to_send = [sys_msg] + state["messages"]
204
  llm_response = llm_with_tools.invoke(messages_to_send)
205
- print(f"LLM Raw Response: {llm_response}")
206
  return {"messages": [llm_response]}
207
 
208
  builder = StateGraph(MessagesState)
209
  builder.add_node("assistant", assistant)
210
- builder.add_node("tools", ToolNode(tools))
211
  builder.add_edge(START, "assistant")
212
  builder.add_conditional_edges("assistant", tools_condition)
213
  builder.add_edge("tools", "assistant")
@@ -215,4 +251,12 @@ def build_graph(provider: str = "gemini"):
215
  return builder.compile()
216
 
217
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
218
  pass
 
3
  import contextlib
4
  import pandas as pd
5
  from typing import Dict, List, Union
6
+ import re # Import regex module to help identify YouTube URLs
7
 
8
  from PIL import Image as PILImage
9
  from huggingface_hub import InferenceClient
 
160
  except Exception as e:
161
  return {"error": f"Error transcribing audio {audio_path}: {str(e)}"}
162
 
163
+ # --- NEW YOUTUBE TOOL ---
164
+ @tool
165
+ def Youtube(url: str, question: str) -> Dict[str, str]:
166
+ """
167
+ Tells about the YouTube video identified by the given URL, answering a question about it.
168
+ Note: This is a simulated response. In a real application, this would interact with a YouTube API
169
+ or a video analysis service to get actual video information and transcripts.
170
+ """
171
+ print(f"Youtube called with URL: {url}, Question: {question}")
172
+
173
+ # Placeholder for actual YouTube API call.
174
+ # In a real scenario, you'd use a library like `google-api-python-client` for YouTube Data API
175
+ # or a dedicated video transcription/analysis service.
176
+
177
+ # Simulating the previous video content for demonstration
178
+ if "https://www.youtube.com/watch?v=1htKBjuUWec" in url or re.search(r'youtube\.com/watch\?v=|youtu\.be/', url):
179
+ return {
180
+ "video_url": url,
181
+ "question_asked": question,
182
+ "video_summary": "The video titled 'Teal'c coffee first time' shows a scene where several individuals are reacting to a beverage, presumably coffee, that Teal'c is trying for the first time. Key moments include: A person off-screen remarking, 'Wow this coffee's great'; another asking if it's 'cinnamon chicory tea oak'; and Teal'c reacting strongly to the taste or temperature, stating 'isn't that hot' indicating he finds it very warm.",
183
+ "details": {
184
+ "00:00:00": "Someone remarks, 'Wow this coffee's great I was just thinking that yeah is that cinnamon chicory tea oak'",
185
+ "00:00:11": "Teal'c takes a large gulp from a black mug",
186
+ "00:00:24": "Teal'c reacts strongly, someone asks 'isn't that hot'",
187
+ "00:00:26": "Someone agrees, 'extremely'"
188
+ }
189
+ }
190
+ else:
191
+ return {"error": "Invalid or unrecognized YouTube URL.", "url": url}
192
+
193
+ # --- END NEW YOUTUBE TOOL ---
194
+
195
  API_KEY = os.getenv("GEMINI_API_KEY")
196
+ HF_API_TOKEN = os.getenv("HF_SPACE_TOKEN") # This seems to be a duplicate or slightly different HF token var
197
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # This is fine
198
 
199
+ # Update the tools list to include the new YouTube tool
200
  tools = [
201
  multiply, add, subtract, divide, modulus,
202
  wiki_search,
 
206
  python_interpreter,
207
  describe_image,
208
  transcribe_audio,
209
+ Youtube, # <-- ADDED THE NEW YOUTUBE TOOL HERE
210
  ]
211
 
212
  with open("prompt.txt", "r", encoding="utf-8") as f:
 
232
  else:
233
  raise ValueError("Invalid provider. Choose 'gemini' or 'huggingface'.")
234
 
235
+ # This is the crucial line that binds your defined Python tools to the LLM
236
  llm_with_tools = llm.bind_tools(tools)
237
 
238
  def assistant(state: MessagesState):
239
  messages_to_send = [sys_msg] + state["messages"]
240
  llm_response = llm_with_tools.invoke(messages_to_send)
241
+ print(f"LLM Raw Response: {llm_response}") # Good for debugging
242
  return {"messages": [llm_response]}
243
 
244
  builder = StateGraph(MessagesState)
245
  builder.add_node("assistant", assistant)
246
+ builder.add_node("tools", ToolNode(tools)) # Ensure ToolNode also has access to all tools
247
  builder.add_edge(START, "assistant")
248
  builder.add_conditional_edges("assistant", tools_condition)
249
  builder.add_edge("tools", "assistant")
 
251
  return builder.compile()
252
 
253
  if __name__ == "__main__":
254
+ # Example usage (you'll need to set GEMINI_API_KEY and potentially HF_API_TOKEN env vars)
255
+ # This part assumes you have a prompt.txt file with the system_prompt as discussed earlier.
256
+
257
+ # You would typically interact with the compiled graph like this:
258
+ # graph = build_graph("gemini")
259
+ # user_input = "Tell me about this video: https://www.youtube.com/watch?v=1htKBjuUWec"
260
+ # result = graph.invoke({"messages": [HumanMessage(content=user_input)]})
261
+ # print(result)
262
  pass