Humanlearning commited on
Commit
f79d6c4
·
1 Parent(s): c9ab476

+ download of csv

Browse files
.gitignore CHANGED
@@ -1,2 +1,4 @@
1
  env*
2
- .env*
 
 
 
1
  env*
2
+ .env*
3
+ review.csv
4
+ *.csv
__pycache__/langraph_agent.cpython-313.pyc CHANGED
Binary files a/__pycache__/langraph_agent.cpython-313.pyc and b/__pycache__/langraph_agent.cpython-313.pyc differ
 
app.py CHANGED
@@ -9,6 +9,7 @@ import asyncio
9
  import aiohttp
10
  from langfuse.langchain import CallbackHandler
11
  from langchain_core.messages import HumanMessage
 
12
 
13
  # Initialize Langfuse CallbackHandler for LangGraph/Langchain (tracing)
14
  langfuse_handler = CallbackHandler()
@@ -55,7 +56,12 @@ async def generate_answers(profile: gr.OAuthProfile | None, progress=gr.Progress
55
  print(f"User logged in: {username}")
56
  else:
57
  print("User not logged in.")
58
- return "Please Login to Hugging Face with the button.", None, gr.update(interactive=False), gr.update(value=0, visible=False)
 
 
 
 
 
59
  api_url = DEFAULT_API_URL
60
  questions_url = f"{api_url}/questions"
61
  try:
@@ -64,11 +70,21 @@ async def generate_answers(profile: gr.OAuthProfile | None, progress=gr.Progress
64
  questions_data = response.json()
65
  if not questions_data:
66
  print("Fetched questions list is empty.")
67
- return "Fetched questions list is empty or invalid format.", None, gr.update(interactive=False), gr.update(value=0, visible=False)
 
 
 
 
 
68
  print(f"Fetched {len(questions_data)} questions.")
69
  except Exception as e:
70
  print(f"Error fetching questions: {e}")
71
- return f"Error fetching questions: {e}", None, gr.update(interactive=False), gr.update(value=0, visible=False)
 
 
 
 
 
72
  agent = BasicAgent()
73
  results_log = []
74
  answers_payload = []
@@ -105,7 +121,23 @@ async def generate_answers(profile: gr.OAuthProfile | None, progress=gr.Progress
105
  cached_results_log = results_log
106
  progress(100, desc="Done.")
107
  results_df = pd.DataFrame(results_log)
108
- return "Answer generation complete. Review and submit.", results_df, gr.update(interactive=True), gr.update(value=100, visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  def submit_answers(profile: gr.OAuthProfile | None):
111
  """
@@ -187,17 +219,20 @@ with gr.Blocks() as demo:
187
  status_output = gr.Textbox(label="Status / Submission Result", lines=5, interactive=False)
188
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
189
 
 
 
 
190
  generate_button.click(
191
  fn=generate_answers,
192
  inputs=[],
193
- outputs=[status_output, results_table, submit_button],
194
  api_name="generate_answers"
195
  )
196
  submit_button.click(
197
  fn=submit_answers,
198
  inputs=[],
199
  outputs=[status_output, results_table],
200
- api_name="submit_answers"
201
  )
202
 
203
  if __name__ == "__main__":
 
9
  import aiohttp
10
  from langfuse.langchain import CallbackHandler
11
  from langchain_core.messages import HumanMessage
12
+ import tempfile
13
 
14
  # Initialize Langfuse CallbackHandler for LangGraph/Langchain (tracing)
15
  langfuse_handler = CallbackHandler()
 
56
  print(f"User logged in: {username}")
57
  else:
58
  print("User not logged in.")
59
+ return (
60
+ "Please Login to Hugging Face with the button.",
61
+ None,
62
+ gr.update(interactive=False), # Disable submit button
63
+ gr.update(value=None, visible=False), # Hide download button
64
+ )
65
  api_url = DEFAULT_API_URL
66
  questions_url = f"{api_url}/questions"
67
  try:
 
70
  questions_data = response.json()
71
  if not questions_data:
72
  print("Fetched questions list is empty.")
73
+ return (
74
+ "Fetched questions list is empty or invalid format.",
75
+ None,
76
+ gr.update(interactive=False),
77
+ gr.update(value=None, visible=False),
78
+ )
79
  print(f"Fetched {len(questions_data)} questions.")
80
  except Exception as e:
81
  print(f"Error fetching questions: {e}")
82
+ return (
83
+ f"Error fetching questions: {e}",
84
+ None,
85
+ gr.update(interactive=False),
86
+ gr.update(value=None, visible=False),
87
+ )
88
  agent = BasicAgent()
89
  results_log = []
90
  answers_payload = []
 
121
  cached_results_log = results_log
122
  progress(100, desc="Done.")
123
  results_df = pd.DataFrame(results_log)
124
+
125
+ # Save answers to a temporary CSV so user can download
126
+ try:
127
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w", newline="", encoding="utf-8") as tmp_csv:
128
+ results_df.to_csv(tmp_csv.name, index=False)
129
+ csv_path = tmp_csv.name
130
+ print(f"CSV saved to {csv_path}")
131
+ except Exception as e:
132
+ print(f"Failed to write CSV: {e}")
133
+ csv_path = None
134
+
135
+ return (
136
+ "Answer generation complete. Review and submit.",
137
+ results_df,
138
+ gr.update(interactive=True), # Enable submit button
139
+ gr.update(value=csv_path, visible=bool(csv_path)), # Show download button if csv written
140
+ )
141
 
142
  def submit_answers(profile: gr.OAuthProfile | None):
143
  """
 
219
  status_output = gr.Textbox(label="Status / Submission Result", lines=5, interactive=False)
220
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
221
 
222
+ # Download button appears after answers are generated
223
+ download_button = gr.DownloadButton(label="Download Answers CSV", visible=False)
224
+
225
  generate_button.click(
226
  fn=generate_answers,
227
  inputs=[],
228
+ outputs=[status_output, results_table, submit_button, download_button],
229
  api_name="generate_answers"
230
  )
231
  submit_button.click(
232
  fn=submit_answers,
233
  inputs=[],
234
  outputs=[status_output, results_table],
235
+ api_name="submit_answers",
236
  )
237
 
238
  if __name__ == "__main__":