fix_threading_issue.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can explain a poorly worded error message in a short threaded python program."
  3. TAGS = ['code', 'python', 'explain']
  4. question = """In this program
  5. from multiprocessing import Pool
  6. from openai import OpenAI
  7. txt = open("/tmp/aa.txt").read().split("\n\n")
  8. from multiprocessing import Pool
  9. import subprocess
  10. client = OpenAI(api_key="sk-XXXXXXXXXXXXXX")
  11. def create_speech_file(index, text):
  12. response = client.audio.speech.create(
  13. model="tts-1-hd",
  14. voice="nova",
  15. input=text
  16. )
  17. filename = f"output{index}.mp3"
  18. response.stream_to_file(filename)
  19. return filename
  20. def merge_mp3(files, output):
  21. with open("filelist.txt", "w") as file:
  22. for f in files:
  23. file.write(f"file '{f}'\n")
  24. cmd = ["ffmpeg", "-f", "concat", "-safe", "0", "-i", "filelist.txt", "-c", "copy", output]
  25. subprocess.run(cmd)
  26. if __name__ == '__main__':
  27. # Step 1: Parallelize speech file creation
  28. with Pool(8) as pool:
  29. mp3_files = pool.starmap(create_speech_file, enumerate(txt))
  30. # Step 2: Merge the generated MP3 files
  31. output_file = "merged_output.mp3"
  32. merge_mp3(mp3_files, output_file)
  33. Why am I getting this error?
  34. Exception in thread Thread-3:
  35. Traceback (most recent call last):
  36. File "/opt/homebrew/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 980, in _bootstrap_inner
  37. self.run()
  38. File "/opt/homebrew/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 917, in run
  39. self._target(*self._args, **self._kwargs)
  40. File "/opt/homebrew/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/pool.py", line 576, in _handle_results
  41. task = get()
  42. File "/opt/homebrew/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 251, in recv
  43. return _ForkingPickler.loads(buf.getbuffer())
  44. TypeError: __init__() missing 2 required keyword-only arguments: 'response' and 'body'
  45. """
  46. TestQuestionThreadedFix = question >> LLMRun() >> LLMRun("I showed a student some code with a bug. I then tested their understanding by asking them to explain hwo to fix the bug. I'm going to show you their answer, I want you to evaluate whether or not their answer says something to the effect of \"The function create_speech_file is raising an exception that can not be pickled.\". Do not judge their reasonining, or if they know how to fix it, just tell me if they correctly identified that there is a crash in the create_speech_file function. \n Student Answer: \"<A>\"\n\n Think out loud then answer either \"The student passes\" or \"The student fails\".", llm=EVAL_LLM) >> SubstringEvaluator("student passes")
  47. if __name__ == "__main__":
  48. print(run_test(TestQuestionThreadedFix))