Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,48 @@
|
|
1 |
-
import gradio as gr
|
2 |
import os
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
|
|
5 |
def upload_file(file):
|
6 |
-
file_path =
|
7 |
with open(file_path, "wb") as f:
|
8 |
f.write(file.read())
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
|
12 |
-
|
|
|
13 |
fn=upload_file,
|
14 |
-
inputs="file",
|
15 |
outputs="text",
|
16 |
-
|
17 |
-
|
18 |
)
|
19 |
|
20 |
-
|
|
|
|
|
|
1 |
import os
|
2 |
+
import aiohttp
|
3 |
+
import asyncio
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Função para dividir o arquivo em chunks
|
7 |
+
def chunk_file(file_path, chunk_size=50*1024*1024):
|
8 |
+
chunks = []
|
9 |
+
with open(file_path, 'rb') as f:
|
10 |
+
while chunk := f.read(chunk_size):
|
11 |
+
chunks.append(chunk)
|
12 |
+
return chunks
|
13 |
+
|
14 |
+
# Função para fazer o upload de um chunk
|
15 |
+
async def upload_chunk(session, url, chunk, chunk_number):
|
16 |
+
async with session.post(url, data={'file': chunk, 'chunk_number': chunk_number}) as response:
|
17 |
+
return await response.text()
|
18 |
+
|
19 |
+
# Função principal para fazer o upload dos chunks em paralelo
|
20 |
+
async def upload_file_chunks(file_path):
|
21 |
+
upload_url = "https://<your_space_name>.hf.space/upload"
|
22 |
+
chunks = chunk_file(file_path)
|
23 |
+
async with aiohttp.ClientSession() as session:
|
24 |
+
tasks = [upload_chunk(session, upload_url, chunk, i) for i, chunk in enumerate(chunks)]
|
25 |
+
results = await asyncio.gather(*tasks)
|
26 |
+
return results
|
27 |
|
28 |
+
# Função chamada pelo Gradio
|
29 |
def upload_file(file):
|
30 |
+
file_path = os.path.join("uploads", file.name)
|
31 |
with open(file_path, "wb") as f:
|
32 |
f.write(file.read())
|
33 |
+
loop = asyncio.new_event_loop()
|
34 |
+
asyncio.set_event_loop(loop)
|
35 |
+
result = loop.run_until_complete(upload_file_chunks(file_path))
|
36 |
+
return f"Upload completed: {result}"
|
37 |
|
38 |
+
# Interface do Gradio
|
39 |
+
demo = gr.Interface(
|
40 |
fn=upload_file,
|
41 |
+
inputs=gr.inputs.File(label="Upload a file"),
|
42 |
outputs="text",
|
43 |
+
live=True,
|
44 |
+
allow_flagging="never"
|
45 |
)
|
46 |
|
47 |
+
if __name__ == "__main__":
|
48 |
+
demo.launch(share=True)
|