philincloud commited on
Commit
c51685e
·
verified ·
1 Parent(s): 7951f5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -2,6 +2,8 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
 
 
5
  from langchain_core.messages import HumanMessage
6
  from langgraph_agent import build_graph
7
 
@@ -101,6 +103,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
101
  # 3. Run your Agent
102
  results_log = []
103
  answers_payload = []
 
104
  print(f"Running agent on {len(questions_data)} questions...")
105
  for item in questions_data:
106
  task_id = item.get("task_id")
@@ -110,10 +113,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
110
  continue
111
  try:
112
  submitted_answer = agent(question_text, task_id=task_id)
113
- print(f"Submitting answer for task {task_id}: '{submitted_answer}'")
114
  answers_payload.append({
115
  "task_id": task_id,
116
- "submitted_answer": submitted_answer
 
117
  })
118
  results_log.append({
119
  "Task ID": task_id,
@@ -122,6 +126,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
122
  })
123
  except Exception as e:
124
  print(f"Error running agent on task {task_id}: {e}")
 
 
 
 
 
125
  results_log.append({
126
  "Task ID": task_id,
127
  "Question": question_text,
@@ -132,18 +141,28 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
132
  print("Agent did not produce any answers to submit.")
133
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
134
 
135
- # 4. Prepare Submission
136
- submission_data = {
 
 
 
 
 
 
 
 
 
 
137
  "username": username.strip(),
138
- "agent_code": agent_code,
139
- "answers": answers_payload
140
  }
 
141
  print(f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'...")
142
 
143
  # 5. Submit
144
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
145
  try:
146
- response = requests.post(submit_url, json=submission_data, timeout=60)
147
  response.raise_for_status()
148
  result_data = response.json()
149
  final_status = (
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ import json
6
+ import io
7
  from langchain_core.messages import HumanMessage
8
  from langgraph_agent import build_graph
9
 
 
103
  # 3. Run your Agent
104
  results_log = []
105
  answers_payload = []
106
+
107
  print(f"Running agent on {len(questions_data)} questions...")
108
  for item in questions_data:
109
  task_id = item.get("task_id")
 
113
  continue
114
  try:
115
  submitted_answer = agent(question_text, task_id=task_id)
116
+ print(f"Answer for task {task_id}: '{submitted_answer}'")
117
  answers_payload.append({
118
  "task_id": task_id,
119
+ "model_answer": submitted_answer,
120
+ # "reasoning_trace": None # Optional: add reasoning trace if available
121
  })
122
  results_log.append({
123
  "Task ID": task_id,
 
126
  })
127
  except Exception as e:
128
  print(f"Error running agent on task {task_id}: {e}")
129
+ answers_payload.append({
130
+ "task_id": task_id,
131
+ "model_answer": f"AGENT ERROR: {e}",
132
+ # "reasoning_trace": None
133
+ })
134
  results_log.append({
135
  "Task ID": task_id,
136
  "Question": question_text,
 
141
  print("Agent did not produce any answers to submit.")
142
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
143
 
144
+ # Serialize to JSON Lines string
145
+ json_lines_str = "\n".join(json.dumps(ans) for ans in answers_payload)
146
+
147
+ # Prepare file-like object for upload
148
+ file_like = io.BytesIO(json_lines_str.encode("utf-8"))
149
+ file_like.name = "submission.jsonl" # Filename required for multipart upload
150
+
151
+ # Prepare multipart form data
152
+ files = {
153
+ "file": (file_like.name, file_like, "application/jsonl")
154
+ }
155
+ data = {
156
  "username": username.strip(),
157
+ "agent_code": agent_code
 
158
  }
159
+
160
  print(f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'...")
161
 
162
  # 5. Submit
163
+ print(f"Submitting answers to: {submit_url}")
164
  try:
165
+ response = requests.post(submit_url, data=data, files=files, timeout=60)
166
  response.raise_for_status()
167
  result_data = response.json()
168
  final_status = (