pkduongsu commited on
Commit
0865717
·
1 Parent(s): eae3ca8

added file download, but all file processing tools are not working

Browse files
Files changed (5) hide show
  1. .gitignore +1 -1
  2. agent.py +1 -3
  3. app.py +21 -9
  4. testing.py +0 -3
  5. tools/read_file.py +0 -26
.gitignore CHANGED
@@ -1,3 +1,3 @@
1
-
2
  env/
3
  __pycache__/
 
1
+ .env
2
  env/
3
  __pycache__/
agent.py CHANGED
@@ -17,9 +17,8 @@ from tools.analyze_csv import analyze_csv
17
  from tools.analyze_excel import analyze_excel
18
  from tools.download_file import download_file
19
  from tools.analyze_image import analyze_image
20
- from tools.read_file import read_and_save_file
21
  from tools.analyze_audio import analyze_audio
22
- from tools.analyze_youtube import answer_question_about_youtube_video
23
  #switch to using gemini 2.0 model
24
  from langchain_google_genai import ChatGoogleGenerativeAI
25
  from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage
@@ -42,7 +41,6 @@ tools = [
42
  analyze_excel,
43
  download_file,
44
  analyze_image,
45
- read_and_save_file,
46
  analyze_audio,
47
  answer_question_about_youtube_video,]
48
 
 
17
  from tools.analyze_excel import analyze_excel
18
  from tools.download_file import download_file
19
  from tools.analyze_image import analyze_image
 
20
  from tools.analyze_audio import analyze_audio
21
+ from tools.analyze_youtube import answer_question_about_youtube_video # Importing YouTube analysis toolS
22
  #switch to using gemini 2.0 model
23
  from langchain_google_genai import ChatGoogleGenerativeAI
24
  from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage
 
41
  analyze_excel,
42
  download_file,
43
  analyze_image,
 
44
  analyze_audio,
45
  answer_question_about_youtube_video,]
46
 
app.py CHANGED
@@ -2,11 +2,11 @@ import os
2
  import gradio as gr
3
  import requests
4
  import re
5
- import asyncio
6
- import inspect
7
  import pandas as pd
8
  from agent import create_agent
9
  from langchain_core.messages import SystemMessage, HumanMessage
 
10
 
11
 
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -88,23 +88,35 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
88
  try:
89
  file_response = requests.get(files_url, timeout=10)
90
  if file_response.status_code == 200:
91
- # Assuming the response body directly contains the filename/path
92
- file_path = file_response.text.strip().strip('"') # Get path and remove potential quotes
93
- print(f"Task {task_id}: Found associated file '{file_path}'")
 
 
 
 
 
 
 
 
 
 
 
94
  elif file_response.status_code == 404:
95
  print(f"Task {task_id}: No associated file found.")
96
- else:
97
  # Log other non-404 errors but don't stop the process
98
- print(f"Task {task_id}: Warning - Error checking for file ({file_response.status_code}): {file_response.text[:100]}")
99
  except requests.exceptions.RequestException as file_err:
100
  print(f"Task {task_id}: Warning - Network error checking for file: {file_err}")
101
 
 
 
102
  # --- Prepare agent input ---
103
  agent_input = {
104
  "messages": [system_message, HumanMessage(content=question_text)]
105
  }
106
- if file_path:
107
- agent_input["input_file"] = file_path # Add file path if found
108
 
109
  # --- Invoke Agent ---
110
  agent_response = agent.invoke(agent_input)
 
2
  import gradio as gr
3
  import requests
4
  import re
5
+ import tempfile
 
6
  import pandas as pd
7
  from agent import create_agent
8
  from langchain_core.messages import SystemMessage, HumanMessage
9
+ from tools.download_file import download_file
10
 
11
 
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
88
  try:
89
  file_response = requests.get(files_url, timeout=10)
90
  if file_response.status_code == 200:
91
+ file = response.headers.get("content-disposition", "")
92
+ match = re.search(r'filename="([^"]+)"', file)
93
+ if match:
94
+ filename = match.group(1)
95
+
96
+ #save file to a temporary location
97
+ with tempfile.NamedTemporaryFile(delete=False) as temp_file:
98
+ temp_file.write(file_response.content)
99
+ file_path = temp_file.name
100
+
101
+ file_prompt = "The file needed for this task is downloaded and saved locally to: " + file_path + ".Read this file to process its content."
102
+
103
+
104
+ print(f"Task {task_id}: Found associated file")
105
  elif file_response.status_code == 404:
106
  print(f"Task {task_id}: No associated file found.")
107
+ else:
108
  # Log other non-404 errors but don't stop the process
109
+ print(f"Task {task_id}: Warning - Error checking for file")
110
  except requests.exceptions.RequestException as file_err:
111
  print(f"Task {task_id}: Warning - Network error checking for file: {file_err}")
112
 
113
+ question_text = question_text + " " + file_prompt if file_path else question_text
114
+
115
  # --- Prepare agent input ---
116
  agent_input = {
117
  "messages": [system_message, HumanMessage(content=question_text)]
118
  }
119
+
 
120
 
121
  # --- Invoke Agent ---
122
  agent_response = agent.invoke(agent_input)
testing.py DELETED
@@ -1,3 +0,0 @@
1
- from tools.analyze_excel import analyze_excel
2
-
3
- from langchain_google_genai import ChatGoogleGenerativeAI
 
 
 
 
tools/read_file.py DELETED
@@ -1,26 +0,0 @@
1
- import os
2
- import tempfile
3
- from langchain.tools import tool
4
-
5
- @tool("read_and_save_file", return_direct=True)
6
- def read_and_save_file(file_path: str) -> str:
7
- """
8
- Reads the content of a file, saves it to a temporary file, and returns the path to the temporary file.
9
-
10
- Args:
11
- file_path (str): The path of the file to read.
12
-
13
- Returns:
14
- str: The path to the temporary file containing the content.
15
- """
16
- if not os.path.exists(file_path):
17
- raise FileNotFoundError(f"The file at {file_path} does not exist.")
18
-
19
- with open(file_path, 'r', encoding='utf-8') as f:
20
- content = f.read()
21
-
22
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode='w', encoding='utf-8')
23
- temp_file.write(content)
24
- temp_file.close()
25
-
26
- return "File read and saved successfullly to {file_path}. Read this file to process its content."