Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ import gradio as gr
|
|
| 7 |
import pandas as pd
|
| 8 |
from duckduckgo_search import DDGS
|
| 9 |
from transformers import pipeline
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -18,21 +20,35 @@ class BasicAgent:
|
|
| 18 |
# Initialize the Whisper model for video transcription
|
| 19 |
self.whisper_model = whisper.load_model("base") # You can change the model to `large`, `medium`, etc.
|
| 20 |
self.search_pipeline = pipeline("question-answering")
|
|
|
|
| 21 |
|
| 22 |
-
def
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def search(self, question: str) -> str:
|
| 33 |
-
|
| 34 |
with DDGS() as ddgs:
|
| 35 |
-
results = list(ddgs.text(question, max_results=3))
|
| 36 |
if results:
|
| 37 |
# Score all the results and return the best one
|
| 38 |
return self.score_search_results(question, results)
|
|
@@ -40,10 +56,16 @@ class BasicAgent:
|
|
| 40 |
return "No relevant search results found."
|
| 41 |
except Exception as e:
|
| 42 |
return f"Search error: {e}"
|
| 43 |
-
|
| 44 |
-
def
|
| 45 |
-
#
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def __call__(self, question: str, video_path: str = None) -> str:
|
| 49 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
@@ -194,7 +216,6 @@ with gr.Blocks() as demo:
|
|
| 194 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 195 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 196 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 197 |
-
|
| 198 |
---
|
| 199 |
**Disclaimers:**
|
| 200 |
Once clicking on the "submit button, it can take quite some time (this is the time for the agent to go through all the questions).
|
|
|
|
| 7 |
import pandas as pd
|
| 8 |
from duckduckgo_search import DDGS
|
| 9 |
from transformers import pipeline
|
| 10 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 11 |
+
import numpy as np
|
| 12 |
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 20 |
# Initialize the Whisper model for video transcription
|
| 21 |
self.whisper_model = whisper.load_model("base") # You can change the model to `large`, `medium`, etc.
|
| 22 |
self.search_pipeline = pipeline("question-answering")
|
| 23 |
+
self.nlp_model = pipeline("feature-extraction") # For semantic similarity (using transformer model)
|
| 24 |
|
| 25 |
+
def score_search_results(self, question: str, search_results: list) -> str:
|
| 26 |
+
# Transform the question and results to embeddings (vector representations)
|
| 27 |
+
question_embedding = self.nlp_model(question)
|
| 28 |
+
question_embedding = np.mean(question_embedding[0], axis=0)
|
| 29 |
+
|
| 30 |
+
best_score = -1
|
| 31 |
+
best_answer = None
|
| 32 |
+
|
| 33 |
+
# Loop through search results and calculate similarity
|
| 34 |
+
for result in search_results:
|
| 35 |
+
result_embedding = self.nlp_model(result['body'])
|
| 36 |
+
result_embedding = np.mean(result_embedding[0], axis=0)
|
| 37 |
+
|
| 38 |
+
# Calculate cosine similarity
|
| 39 |
+
similarity = cosine_similarity([question_embedding], [result_embedding])
|
| 40 |
+
|
| 41 |
+
# Check if this result is better
|
| 42 |
+
if similarity > best_score:
|
| 43 |
+
best_score = similarity
|
| 44 |
+
best_answer = result['body']
|
| 45 |
+
|
| 46 |
+
return best_answer
|
| 47 |
|
| 48 |
def search(self, question: str) -> str:
|
| 49 |
+
try:
|
| 50 |
with DDGS() as ddgs:
|
| 51 |
+
results = list(ddgs.text(question, max_results=3)) # Fetch top 3 results
|
| 52 |
if results:
|
| 53 |
# Score all the results and return the best one
|
| 54 |
return self.score_search_results(question, results)
|
|
|
|
| 56 |
return "No relevant search results found."
|
| 57 |
except Exception as e:
|
| 58 |
return f"Search error: {e}"
|
| 59 |
+
|
| 60 |
+
def call_whisper(self, video_path: str) -> str:
|
| 61 |
+
# Transcribe the video to text using Whisper model
|
| 62 |
+
video = moviepy.editor.VideoFileClip(video_path)
|
| 63 |
+
audio_path = "temp_audio.wav"
|
| 64 |
+
video.audio.write_audiofile(audio_path)
|
| 65 |
+
|
| 66 |
+
# Transcribe audio to text
|
| 67 |
+
result = self.whisper_model.transcribe(audio_path)
|
| 68 |
+
return result["text"]
|
| 69 |
|
| 70 |
def __call__(self, question: str, video_path: str = None) -> str:
|
| 71 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
| 216 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 217 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 218 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
| 219 |
---
|
| 220 |
**Disclaimers:**
|
| 221 |
Once clicking on the "submit button, it can take quite some time (this is the time for the agent to go through all the questions).
|