Spaces:
Sleeping
Sleeping
mriusero
commited on
Commit
·
697c6cd
1
Parent(s):
057ef0a
feat: rich it
Browse files- .gitignore +1 -0
- src/inference.py +0 -1
- src/utils/api.py +0 -1
- src/utils/truth.py +0 -0
- src/workflow.py +31 -13
.gitignore
CHANGED
|
@@ -13,6 +13,7 @@
|
|
| 13 |
# Model
|
| 14 |
llm/
|
| 15 |
Attachments/
|
|
|
|
| 16 |
|
| 17 |
# Utils
|
| 18 |
agents_final_questions.json
|
|
|
|
| 13 |
# Model
|
| 14 |
llm/
|
| 15 |
Attachments/
|
| 16 |
+
metadata.jsonl
|
| 17 |
|
| 18 |
# Utils
|
| 19 |
agents_final_questions.json
|
src/inference.py
CHANGED
|
@@ -109,7 +109,6 @@ class Agent:
|
|
| 109 |
function_name = tool_call.function.name
|
| 110 |
function_params = json.loads(tool_call.function.arguments)
|
| 111 |
print("\nfunction_name: ", function_name, "\nfunction_params: ", function_params)
|
| 112 |
-
|
| 113 |
try:
|
| 114 |
function_result = names_to_functions[function_name](**function_params)
|
| 115 |
print("\nfunction_result: ", function_result)
|
|
|
|
| 109 |
function_name = tool_call.function.name
|
| 110 |
function_params = json.loads(tool_call.function.arguments)
|
| 111 |
print("\nfunction_name: ", function_name, "\nfunction_params: ", function_params)
|
|
|
|
| 112 |
try:
|
| 113 |
function_result = names_to_functions[function_name](**function_params)
|
| 114 |
print("\nfunction_result: ", function_result)
|
src/utils/api.py
CHANGED
|
@@ -12,7 +12,6 @@ def fetch_questions(api_url=DEFAULT_API_URL):
|
|
| 12 |
list: A list of questions if successful, None otherwise.
|
| 13 |
"""
|
| 14 |
questions_url = f"{api_url}/questions"
|
| 15 |
-
print(f"Fetching questions from: {questions_url}")
|
| 16 |
try:
|
| 17 |
response = requests.get(questions_url, timeout=15)
|
| 18 |
response.raise_for_status()
|
|
|
|
| 12 |
list: A list of questions if successful, None otherwise.
|
| 13 |
"""
|
| 14 |
questions_url = f"{api_url}/questions"
|
|
|
|
| 15 |
try:
|
| 16 |
response = requests.get(questions_url, timeout=15)
|
| 17 |
response.raise_for_status()
|
src/utils/truth.py
DELETED
|
File without changes
|
src/workflow.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
import re
|
|
|
|
|
|
|
| 6 |
|
| 7 |
from src.utils import (
|
| 8 |
fetch_questions,
|
|
@@ -12,19 +15,19 @@ from src.utils import (
|
|
| 12 |
from src.inference import Agent
|
| 13 |
|
| 14 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 15 |
-
|
| 16 |
agent = Agent()
|
| 17 |
space_id = os.getenv("SPACE_ID")
|
| 18 |
|
| 19 |
if profile:
|
| 20 |
username = f"{profile.username}"
|
| 21 |
-
print(f"User logged in: {username}")
|
| 22 |
else:
|
| 23 |
-
print("User not logged in.")
|
| 24 |
return "Please Login to Hugging Face with the button.", None
|
| 25 |
|
| 26 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 27 |
-
print(agent_code)
|
| 28 |
|
| 29 |
questions_data = fetch_questions()
|
| 30 |
|
|
@@ -35,7 +38,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 35 |
answers_payload = []
|
| 36 |
|
| 37 |
for item in questions_data:
|
| 38 |
-
|
| 39 |
task_id = item.get("task_id")
|
| 40 |
question_text = item.get("question")
|
| 41 |
file_name = item.get("file_name")
|
|
@@ -50,28 +52,44 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 50 |
file_context = ""
|
| 51 |
|
| 52 |
try:
|
| 53 |
-
|
| 54 |
-
print(f"\n
|
|
|
|
| 55 |
|
| 56 |
response = agent.run(input=question_text + file_context)
|
| 57 |
-
match = re.search(r'FINAL ANSWER:\s*(.*)', response, re.DOTALL)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
if match:
|
| 60 |
submitted_answer = match.group(1).strip()
|
| 61 |
else:
|
| 62 |
submitted_answer = 'No FINAL ANSWER found.'
|
| 63 |
|
| 64 |
-
|
| 65 |
-
print(f"\n
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 68 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
except Exception as e:
|
| 71 |
-
print(f"Error: {e}")
|
| 72 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 73 |
|
| 74 |
-
time.sleep(
|
| 75 |
|
| 76 |
if not answers_payload:
|
| 77 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
|
@@ -90,4 +108,4 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 90 |
results_df = pd.DataFrame(results_log)
|
| 91 |
return final_status, results_df
|
| 92 |
else:
|
| 93 |
-
return "Submission Failed.", pd.DataFrame(results_log)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import json
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
import re
|
| 7 |
+
from rich.console import Console
|
| 8 |
+
from rich.panel import Panel
|
| 9 |
|
| 10 |
from src.utils import (
|
| 11 |
fetch_questions,
|
|
|
|
| 15 |
from src.inference import Agent
|
| 16 |
|
| 17 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 18 |
+
console = Console()
|
| 19 |
agent = Agent()
|
| 20 |
space_id = os.getenv("SPACE_ID")
|
| 21 |
|
| 22 |
if profile:
|
| 23 |
username = f"{profile.username}"
|
| 24 |
+
console.print(f"User logged in: {username}", style="bold green")
|
| 25 |
else:
|
| 26 |
+
console.print("User not logged in.", style="bold red")
|
| 27 |
return "Please Login to Hugging Face with the button.", None
|
| 28 |
|
| 29 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 30 |
+
console.print(agent_code)
|
| 31 |
|
| 32 |
questions_data = fetch_questions()
|
| 33 |
|
|
|
|
| 38 |
answers_payload = []
|
| 39 |
|
| 40 |
for item in questions_data:
|
|
|
|
| 41 |
task_id = item.get("task_id")
|
| 42 |
question_text = item.get("question")
|
| 43 |
file_name = item.get("file_name")
|
|
|
|
| 52 |
file_context = ""
|
| 53 |
|
| 54 |
try:
|
| 55 |
+
console.rule(f"\n[bold blue]Task ID: {task_id}")
|
| 56 |
+
console.print(Panel(f"[bold]Question[/bold]\n{question_text}", expand=False))
|
| 57 |
+
console.print(Panel(f"[italic]File Context[/italic]\n{file_context}", expand=False))
|
| 58 |
|
| 59 |
response = agent.run(input=question_text + file_context)
|
|
|
|
| 60 |
|
| 61 |
+
# - See Inference.py for intermediate steps
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
match = re.search(r'FINAL ANSWER:\s*(.*)', response, re.DOTALL)
|
| 65 |
if match:
|
| 66 |
submitted_answer = match.group(1).strip()
|
| 67 |
else:
|
| 68 |
submitted_answer = 'No FINAL ANSWER found.'
|
| 69 |
|
| 70 |
+
|
| 71 |
+
console.print(Panel(f"[bold]Response[/bold]\n{response}", expand=False))
|
| 72 |
+
|
| 73 |
+
# Affichage de la réponse soumise
|
| 74 |
+
console.print(Panel(f"[bold green]Submitted Answer[/bold green]\n{submitted_answer}", expand=False))
|
| 75 |
|
| 76 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 77 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 78 |
|
| 79 |
+
# Vérification des métadonnées
|
| 80 |
+
console.rule("[bold yellow]Check")
|
| 81 |
+
with open('./metadata.jsonl', 'r') as file:
|
| 82 |
+
for line in file:
|
| 83 |
+
item = json.loads(line)
|
| 84 |
+
if item.get('task_id') == task_id:
|
| 85 |
+
final_answer = item.get('Final answer')
|
| 86 |
+
console.print(f"Final answer pour la task_id : [bold]{final_answer}[/bold]")
|
| 87 |
+
|
| 88 |
except Exception as e:
|
| 89 |
+
console.print(f"Error: {e}", style="bold red")
|
| 90 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 91 |
|
| 92 |
+
time.sleep(1) # Rate limit for API calls
|
| 93 |
|
| 94 |
if not answers_payload:
|
| 95 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
|
|
|
| 108 |
results_df = pd.DataFrame(results_log)
|
| 109 |
return final_status, results_df
|
| 110 |
else:
|
| 111 |
+
return "Submission Failed.", pd.DataFrame(results_log)
|