Spaces:
Configuration error
Configuration error
Download question_analyzer.py from reekuzz/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 6.07 kB
-
https://huggingface.co/spaces/reekuzz/Final_Assignment_Template/resolve/main/question_analyzer.py
- Command line
-
hf download hf://spaces/reekuzz/Final_Assignment_Template/question_analyzer.py
-
curl -L -o question_analyzer.py https://huggingface.co/spaces/reekuzz/Final_Assignment_Template/resolve/main/question_analyzer.py
6.07 kB
| import requests | |
| import json | |
| import pandas as pd | |
| from typing import List, Dict, Any | |
| class QuestionAnalyzer: | |
| def __init__(self, api_url: str = "https://agents-course-unit4-scoring.hf.space"): | |
| self.api_url = api_url | |
| self.questions_url = f"{api_url}/questions" | |
| self.questions_data = None | |
| def fetch_questions(self) -> bool: | |
| """ | |
| Recupera le domande dal server e le salva nell'istanza | |
| Returns: True se il fetch Γ¨ andato a buon fine, False altrimenti | |
| """ | |
| print(f"Fetching questions from: {self.questions_url}") | |
| try: | |
| response = requests.get(self.questions_url, timeout=15) | |
| response.raise_for_status() | |
| self.questions_data = response.json() | |
| if not self.questions_data: | |
| print("β Questions list is empty.") | |
| return False | |
| print(f"β Successfully fetched {len(self.questions_data)} questions.") | |
| return True | |
| except requests.exceptions.RequestException as e: | |
| print(f"β Error fetching questions: {e}") | |
| return False | |
| except json.JSONDecodeError as e: | |
| print(f"β Error decoding JSON response: {e}") | |
| return False | |
| except Exception as e: | |
| print(f"β Unexpected error: {e}") | |
| return False | |
| def analyze_structure(self) -> None: | |
| """Analizza la struttura delle domande""" | |
| if not self.questions_data: | |
| print("β No questions data available. Run fetch_questions() first.") | |
| return | |
| print("\n" + "="*50) | |
| print("QUESTION STRUCTURE ANALYSIS") | |
| print("="*50) | |
| # Analisi generale | |
| print(f"π Total questions: {len(self.questions_data)}") | |
| if len(self.questions_data) > 0: | |
| # Analizza il primo elemento per capire la struttura | |
| first_question = self.questions_data[0] | |
| print(f"\nπ Keys in each question: {list(first_question.keys())}") | |
| # Mostra il primo esempio completo | |
| print(f"\nπ First question example:") | |
| for key, value in first_question.items(): | |
| if isinstance(value, str) and len(value) > 100: | |
| print(f" {key}: {value[:100]}...") | |
| else: | |
| print(f" {key}: {value}") | |
| def show_sample_questions(self, n: int = 5) -> None: | |
| """Mostra n domande di esempio""" | |
| if not self.questions_data: | |
| print("β No questions data available. Run fetch_questions() first.") | |
| return | |
| print(f"\n" + "="*50) | |
| print(f"SAMPLE QUESTIONS (showing first {min(n, len(self.questions_data))})") | |
| print("="*50) | |
| for i, question in enumerate(self.questions_data[:n]): | |
| print(f"\n--- Question {i+1} ---") | |
| task_id = question.get('task_id', 'N/A') | |
| question_text = question.get('question', 'N/A') | |
| print(f"Task ID: {task_id}") | |
| print(f"Question: {question_text[:200]}{'...' if len(question_text) > 200 else ''}") | |
| # Mostra altri campi se presenti | |
| for key, value in question.items(): | |
| if key not in ['task_id', 'question']: | |
| if isinstance(value, str) and len(value) > 50: | |
| print(f"{key}: {value[:50]}...") | |
| else: | |
| print(f"{key}: {value}") | |
| def create_summary_dataframe(self) -> pd.DataFrame: | |
| """Crea un DataFrame riassuntivo delle domande""" | |
| if not self.questions_data: | |
| print("β No questions data available. Run fetch_questions() first.") | |
| return pd.DataFrame() | |
| summary_data = [] | |
| for question in self.questions_data: | |
| summary_row = { | |
| 'task_id': question.get('task_id', 'N/A'), | |
| 'question_length': len(question.get('question', '')), | |
| 'question_preview': question.get('question', '')[:100] + '...' if len(question.get('question', '')) > 100 else question.get('question', ''), | |
| } | |
| # Aggiungi altri campi se presenti | |
| for key, value in question.items(): | |
| if key not in ['task_id', 'question']: | |
| summary_row[key] = value | |
| summary_data.append(summary_row) | |
| return pd.DataFrame(summary_data) | |
| def save_questions_to_file(self, filename: str = "questions_data.json") -> bool: | |
| """Salva le domande in un file JSON locale""" | |
| if not self.questions_data: | |
| print("β No questions data available. Run fetch_questions() first.") | |
| return False | |
| try: | |
| with open(filename, 'w', encoding='utf-8') as f: | |
| json.dump(self.questions_data, f, indent=2, ensure_ascii=False) | |
| print(f"β Questions saved to {filename}") | |
| return True | |
| except Exception as e: | |
| print(f"β Error saving questions: {e}") | |
| return False | |
| def main(): | |
| """Funzione principale per testare il QuestionAnalyzer""" | |
| print("π Starting Question Analysis...") | |
| # Crea l'analyzer | |
| analyzer = QuestionAnalyzer() | |
| # Fetch delle domande | |
| if not analyzer.fetch_questions(): | |
| print("β Failed to fetch questions. Exiting.") | |
| return | |
| # Analizza la struttura | |
| analyzer.analyze_structure() | |
| # Mostra domande di esempio | |
| analyzer.show_sample_questions(3) | |
| # Crea e mostra DataFrame riassuntivo | |
| df = analyzer.create_summary_dataframe() | |
| if not df.empty: | |
| print(f"\n" + "="*50) | |
| print("SUMMARY DATAFRAME") | |
| print("="*50) | |
| print(df.head(10)) | |
| # Salva le domande localmente | |
| analyzer.save_questions_to_file() | |
| print(f"\nβ Analysis complete!") | |
| if __name__ == "__main__": | |
| main() |