Spaces:
Running
Running
TeacherPuffy
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_client import Client
|
3 |
+
import os
|
4 |
+
import zipfile
|
5 |
+
from datasets import Dataset
|
6 |
+
from huggingface_hub import HfApi
|
7 |
+
|
8 |
+
# Initialize the Gradio client
|
9 |
+
client = Client("MiniMaxAI/MiniMax-Text-01")
|
10 |
+
|
11 |
+
# Function to call the API and get the result
|
12 |
+
def call_api(prompt):
|
13 |
+
result = client.predict(
|
14 |
+
message=prompt,
|
15 |
+
max_tokens=12800,
|
16 |
+
temperature=0.1,
|
17 |
+
top_p=0.9,
|
18 |
+
api_name="/chat"
|
19 |
+
)
|
20 |
+
return result
|
21 |
+
|
22 |
+
# Function to segment the text file into chunks of 3000 words
|
23 |
+
def segment_text(file_path):
|
24 |
+
with open(file_path, "r") as f:
|
25 |
+
text = f.read()
|
26 |
+
words = text.split()
|
27 |
+
chunks = [" ".join(words[i:i + 3000]) for i in range(0, len(words), 3000)]
|
28 |
+
return chunks
|
29 |
+
|
30 |
+
# Function to process the text file and make parallel API calls
|
31 |
+
def process_text(file, prompt):
|
32 |
+
# Segment the text file into chunks
|
33 |
+
chunks = segment_text(file.name)
|
34 |
+
|
35 |
+
# Perform two parallel API calls for each chunk
|
36 |
+
results = []
|
37 |
+
for chunk in chunks:
|
38 |
+
result1 = call_api(f"{prompt}\n\n{chunk}")
|
39 |
+
result2 = call_api(f"{prompt}\n\n{chunk}")
|
40 |
+
results.extend([result1, result2])
|
41 |
+
|
42 |
+
# Save results as individual text files
|
43 |
+
os.makedirs("outputs", exist_ok=True)
|
44 |
+
for idx, result in enumerate(results):
|
45 |
+
with open(f"outputs/output_{idx}.txt", "w") as f:
|
46 |
+
f.write(result)
|
47 |
+
|
48 |
+
# Upload to Hugging Face dataset
|
49 |
+
hf_api = HfApi(token=os.environ["HUGGINGFACE_TOKEN"])
|
50 |
+
dataset = Dataset.from_dict({"text": results})
|
51 |
+
dataset.push_to_hub("your_huggingface_username/your_dataset_name")
|
52 |
+
|
53 |
+
# Create a ZIP file
|
54 |
+
with zipfile.ZipFile("outputs.zip", "w") as zipf:
|
55 |
+
for root, dirs, files in os.walk("outputs"):
|
56 |
+
for file in files:
|
57 |
+
zipf.write(os.path.join(root, file), file)
|
58 |
+
|
59 |
+
return "outputs.zip", "Results uploaded to Hugging Face dataset and ZIP file created."
|
60 |
+
|
61 |
+
# Gradio interface
|
62 |
+
with gr.Blocks() as demo:
|
63 |
+
gr.Markdown("## Text File Processor with Parallel API Calls")
|
64 |
+
with gr.Row():
|
65 |
+
file_input = gr.File(label="Upload Text File")
|
66 |
+
prompt_input = gr.Textbox(label="Enter Prompt")
|
67 |
+
with gr.Row():
|
68 |
+
output_zip = gr.File(label="Download ZIP File")
|
69 |
+
output_message = gr.Textbox(label="Status Message")
|
70 |
+
submit_button = gr.Button("Submit")
|
71 |
+
|
72 |
+
submit_button.click(
|
73 |
+
process_text,
|
74 |
+
inputs=[file_input, prompt_input],
|
75 |
+
outputs=[output_zip, output_message]
|
76 |
+
)
|
77 |
+
|
78 |
+
# Launch the Gradio app
|
79 |
+
demo.launch()
|