asad231 commited on
Commit
5c58f4c
·
verified ·
1 Parent(s): b775405

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -57,6 +57,7 @@
57
  import os
58
  import gradio as gr
59
  from langchain.text_splitter import RecursiveCharacterTextSplitter
 
60
 
61
  # Ensure data folder exists
62
  os.makedirs("data", exist_ok=True)
@@ -65,26 +66,27 @@ def process_files(italy_file, france_file):
65
  docs = []
66
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=50)
67
 
68
- # Save uploaded files
69
- file_paths = {}
70
  try:
 
 
 
71
  if italy_file is not None:
72
  italy_path = "data/italy.txt"
73
- with open(italy_path, "wb") as f:
74
- f.write(italy_file.read())
75
  file_paths["Italy"] = italy_path
76
 
 
77
  if france_file is not None:
78
  france_path = "data/france.txt"
79
- with open(france_path, "wb") as f:
80
- f.write(france_file.read())
81
  file_paths["France"] = france_path
 
82
  except Exception as e:
83
  return f"❌ Error while saving files: {e}", []
84
 
85
- # Process text from files
86
  try:
87
- for name, path in file_paths.items():
88
  with open(path, "r", encoding="utf-8") as f:
89
  content = f.read()
90
  chunks = text_splitter.create_documents([content])
@@ -103,8 +105,8 @@ gr.Interface(
103
  ],
104
  outputs=[
105
  gr.Textbox(label="Status"),
106
- gr.Textbox(label="First 10 Chunks", lines=20)
107
  ],
108
- title="Text Chunker with LangChain",
109
- description="Upload italy.txt and france.txt to split them into 300-character chunks."
110
  ).launch()
 
57
  import os
58
  import gradio as gr
59
  from langchain.text_splitter import RecursiveCharacterTextSplitter
60
+ import shutil
61
 
62
  # Ensure data folder exists
63
  os.makedirs("data", exist_ok=True)
 
66
  docs = []
67
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=50)
68
 
 
 
69
  try:
70
+ file_paths = {}
71
+
72
+ # Move italy file
73
  if italy_file is not None:
74
  italy_path = "data/italy.txt"
75
+ shutil.copy(italy_file.name, italy_path)
 
76
  file_paths["Italy"] = italy_path
77
 
78
+ # Move france file
79
  if france_file is not None:
80
  france_path = "data/france.txt"
81
+ shutil.copy(france_file.name, france_path)
 
82
  file_paths["France"] = france_path
83
+
84
  except Exception as e:
85
  return f"❌ Error while saving files: {e}", []
86
 
87
+ # Process the files
88
  try:
89
+ for country, path in file_paths.items():
90
  with open(path, "r", encoding="utf-8") as f:
91
  content = f.read()
92
  chunks = text_splitter.create_documents([content])
 
105
  ],
106
  outputs=[
107
  gr.Textbox(label="Status"),
108
+ gr.Textbox(label="First 10 Chunks (Preview)", lines=20)
109
  ],
110
+ title="Text Chunking App (LangChain)",
111
+ description="Upload two .txt files (Italy & France). They will be split into 300-character chunks."
112
  ).launch()