DeepLearning101 commited on
Commit
f1f0d5a
·
verified ·
1 Parent(s): 0ae976a

Update app.py

Browse files

Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/gradio/queueing.py", line 522, in process_events
response = await route_utils.call_process_api(
File "/usr/local/lib/python3.10/site-packages/gradio/route_utils.py", line 260, in call_process_api
output = await app.get_blocks().process_api(
File "/usr/local/lib/python3.10/site-packages/gradio/blocks.py", line 1689, in process_api
result = await self.call_function(
File "/usr/local/lib/python3.10/site-packages/gradio/blocks.py", line 1255, in call_function
prediction = await anyio.to_thread.run_sync(
File "/usr/local/lib/python3.10/site-packages/anyio/to_thread.py", line 56, in run_sync
return await get_async_backend().run_sync_in_worker_thread(
File "/usr/local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 2144, in run_sync_in_worker_thread
return await future
File "/usr/local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 851, in run
result = context.run(func, *args)
File "/usr/local/lib/python3.10/site-packages/gradio/utils.py", line 750, in wrapper
response = f(*args, **kwargs)
File "/home/user/app/app.py", line 9, in compare_texts
correct_text = correct_file.read().decode('utf-8') # 解碼二進制數據
AttributeError: 'bytes' object has no attribute 'read'

在Gradio 中,當檔案上傳元件的type設定為"binary"時,檔案內容以位元組字串(bytes)的形式直接傳遞給函數,而不是檔案路徑或檔案物件。因此,不需要使用.read()方法來讀取內容,因為您已經直接得到了文件的內容。

解決方法
移除.read()呼叫: 由於您已經設定了type="binary",您應該直接處理字節串數據,不需要呼叫.read()。
解碼位元組資料: 使用.decode('utf-8')將位元組資料轉換為字串,以便後續處理。

Files changed (1) hide show
  1. app.py +3 -3
app.py CHANGED
@@ -6,8 +6,8 @@ import difflib
6
 
7
  def compare_texts(correct_file, wrong_file):
8
  """讀取並返回文件內容"""
9
- correct_text = correct_file.read().decode('utf-8') # 解碼二進制數據
10
- wrong_text = wrong_file.read().decode('utf-8') # 解碼二進制數據
11
 
12
  # 比較兩個文本並找出不同的位置
13
  s = difflib.SequenceMatcher(None, wrong_text, correct_text)
@@ -76,4 +76,4 @@ with gr.Blocks() as demo:
76
  outputs=[json_output, json_download_link]
77
  )
78
 
79
- demo.launch(share=True)
 
6
 
7
  def compare_texts(correct_file, wrong_file):
8
  """讀取並返回文件內容"""
9
+ correct_text = correct_file.decode('utf-8')
10
+ wrong_text = wrong_file.decode('utf-8')
11
 
12
  # 比較兩個文本並找出不同的位置
13
  s = difflib.SequenceMatcher(None, wrong_text, correct_text)
 
76
  outputs=[json_output, json_download_link]
77
  )
78
 
79
+ demo.launch()