TeacherPuffy commited on
Commit
39bca12
·
verified ·
1 Parent(s): a937b8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -21,8 +21,16 @@ def call_api(prompt):
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
@@ -42,7 +50,7 @@ def process_text(file, prompt):
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
@@ -75,5 +83,5 @@ with gr.Blocks() as demo:
75
  outputs=[output_zip, output_message]
76
  )
77
 
78
- # Launch the Gradio app
79
- demo.launch()
 
21
 
22
  # Function to segment the text file into chunks of 3000 words
23
  def segment_text(file_path):
24
+ try:
25
+ # Try reading with UTF-8 encoding first
26
+ with open(file_path, "r", encoding="utf-8") as f:
27
+ text = f.read()
28
+ except UnicodeDecodeError:
29
+ # Fallback to latin-1 encoding if UTF-8 fails
30
+ with open(file_path, "r", encoding="latin-1") as f:
31
+ text = f.read()
32
+
33
+ # Split the text into chunks of 3000 words
34
  words = text.split()
35
  chunks = [" ".join(words[i:i + 3000]) for i in range(0, len(words), 3000)]
36
  return chunks
 
50
  # Save results as individual text files
51
  os.makedirs("outputs", exist_ok=True)
52
  for idx, result in enumerate(results):
53
+ with open(f"outputs/output_{idx}.txt", "w", encoding="utf-8") as f:
54
  f.write(result)
55
 
56
  # Upload to Hugging Face dataset
 
83
  outputs=[output_zip, output_message]
84
  )
85
 
86
+ # Launch the Gradio app with a public link
87
+ demo.launch(share=True)