Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,12 +48,17 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 48 |
|
| 49 |
# --- Agent Tools ---
|
| 50 |
def wikipedia_lookup(query):
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
wiki_tool = Tool(
|
| 58 |
name="WikipediaTool",
|
| 59 |
func=wikipedia_lookup,
|
|
@@ -65,6 +70,8 @@ def get_youtube_transcript(url):
|
|
| 65 |
yt = YouTube(url)
|
| 66 |
caption = yt.captions.get_by_language_code('en')
|
| 67 |
return caption.generate_srt_captions()[:2048]
|
|
|
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
return f"Failed to retrieve transcript: {str(e)}"
|
| 70 |
|
|
@@ -75,9 +82,14 @@ youtube_tool = Tool(
|
|
| 75 |
)
|
| 76 |
|
| 77 |
def transcribe_audio(file_path):
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
audio_tool = Tool(
|
| 83 |
name="AudioTranscriber",
|
|
@@ -86,15 +98,20 @@ audio_tool = Tool(
|
|
| 86 |
)
|
| 87 |
|
| 88 |
def extract_food_sales(filepath):
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
excel_tool = Tool(
|
| 99 |
name="ExcelFoodSales",
|
| 100 |
func=extract_food_sales,
|
|
@@ -102,14 +119,19 @@ excel_tool = Tool(
|
|
| 102 |
)
|
| 103 |
|
| 104 |
def describe_image(image_path):
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
image_tool = Tool(
|
| 114 |
name="ImageDescriber",
|
| 115 |
func=describe_image,
|
|
|
|
| 48 |
|
| 49 |
# --- Agent Tools ---
|
| 50 |
def wikipedia_lookup(query):
|
| 51 |
+
try:
|
| 52 |
+
wiki_wiki = wikipediaapi.Wikipedia('en')
|
| 53 |
+
page = wiki_wiki.page(query)
|
| 54 |
+
if not page.exists():
|
| 55 |
+
return f"Wikipedia page for '{query}' not found."
|
| 56 |
+
return page.summary[:1024]
|
| 57 |
+
except StopIteration:
|
| 58 |
+
return "ERROR: YouTube transcript tool raised StopIteration."
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return f"Failed to retrieve transcript: {str(e)}"
|
| 61 |
+
|
| 62 |
wiki_tool = Tool(
|
| 63 |
name="WikipediaTool",
|
| 64 |
func=wikipedia_lookup,
|
|
|
|
| 70 |
yt = YouTube(url)
|
| 71 |
caption = yt.captions.get_by_language_code('en')
|
| 72 |
return caption.generate_srt_captions()[:2048]
|
| 73 |
+
except StopIteration:
|
| 74 |
+
return "ERROR: YouTube transcript tool raised StopIteration."
|
| 75 |
except Exception as e:
|
| 76 |
return f"Failed to retrieve transcript: {str(e)}"
|
| 77 |
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
def transcribe_audio(file_path):
|
| 85 |
+
try:
|
| 86 |
+
model = whisper.load_model("base")
|
| 87 |
+
result = model.transcribe(file_path)
|
| 88 |
+
return result['text'][:2048]
|
| 89 |
+
except StopIteration:
|
| 90 |
+
return "ERROR: YouTube transcript tool raised StopIteration."
|
| 91 |
+
except Exception as e:
|
| 92 |
+
return f"Failed to retrieve transcript: {str(e)}"
|
| 93 |
|
| 94 |
audio_tool = Tool(
|
| 95 |
name="AudioTranscriber",
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
def extract_food_sales(filepath):
|
| 101 |
+
try:
|
| 102 |
+
wb = openpyxl.load_workbook(filepath)
|
| 103 |
+
sheet = wb.active
|
| 104 |
+
total = 0
|
| 105 |
+
for row in sheet.iter_rows(min_row=2, values_only=True):
|
| 106 |
+
item, category, sales = row
|
| 107 |
+
if category.lower() == 'food':
|
| 108 |
+
total += float(sales)
|
| 109 |
+
return f"Total food sales: ${total:.2f}"
|
| 110 |
+
except StopIteration:
|
| 111 |
+
return "ERROR: YouTube transcript tool raised StopIteration."
|
| 112 |
+
except Exception as e:
|
| 113 |
+
return f"Failed to retrieve transcript: {str(e)}"
|
| 114 |
+
|
| 115 |
excel_tool = Tool(
|
| 116 |
name="ExcelFoodSales",
|
| 117 |
func=extract_food_sales,
|
|
|
|
| 119 |
)
|
| 120 |
|
| 121 |
def describe_image(image_path):
|
| 122 |
+
try:
|
| 123 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 124 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 125 |
+
raw_image = Image.open(image_path).convert('RGB')
|
| 126 |
+
inputs = processor(raw_image, return_tensors="pt")
|
| 127 |
+
out = model.generate(**inputs)
|
| 128 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 129 |
+
return caption
|
| 130 |
+
except StopIteration:
|
| 131 |
+
return "ERROR: YouTube transcript tool raised StopIteration."
|
| 132 |
+
except Exception as e:
|
| 133 |
+
return f"Failed to retrieve transcript: {str(e)}"
|
| 134 |
+
|
| 135 |
image_tool = Tool(
|
| 136 |
name="ImageDescriber",
|
| 137 |
func=describe_image,
|