Rafa1986 commited on
Commit
a64a105
·
verified ·
1 Parent(s): be26077

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -8
app.py CHANGED
@@ -23,8 +23,8 @@ def detect_language(text):
23
  openai.api_key = "YOUR_OPENAI_API_KEY"
24
 
25
  def extract_files_from_folder(folder_path):
26
- """Scans a folder and its subfolders for PDF, TXT, and CSV files."""
27
- extracted_files = {"pdf": [], "txt": [], "csv": []}
28
 
29
  for root, _, files in os.walk(folder_path):
30
  for file_name in files:
@@ -35,6 +35,8 @@ def extract_files_from_folder(folder_path):
35
  extracted_files["txt"].append(file_path)
36
  elif file_name.endswith(".csv"):
37
  extracted_files["csv"].append(file_path)
 
 
38
  return extracted_files
39
 
40
  def read_text_from_files(file_paths):
@@ -99,15 +101,12 @@ def get_answer(question, vector_db, corrected_exercises):
99
 
100
  def chatbot_interface(question):
101
  folder_path = "/mnt/data/Data Analitics/"
102
- if not folder_path:
103
- return "Please provide a folder path before asking a question."
104
-
105
  extracted_files = extract_files_from_folder(folder_path)
106
 
107
  text = get_text_from_pdf(extracted_files["pdf"]) + read_text_from_files(extracted_files["txt"]) + get_text_from_csv(extracted_files["csv"])
108
 
109
  if not text:
110
- return "The folder does not contain valid PDF, TXT, or CSV files. Please upload supported file types."
111
 
112
  corrected_exercises = correct_exercises(text)
113
  vector_db = create_vector_database(text)
@@ -116,8 +115,7 @@ def chatbot_interface(question):
116
  # Gradio interface
117
  demo = gr.Interface(
118
  fn=chatbot_interface,
119
- inputs=[gr.Textbox(label="Folder Path", placeholder="Enter the path to the folder containing the documents"),
120
- gr.Textbox(label="Ask a question", placeholder="Type your question here...")],
121
  outputs=gr.Textbox(label="Answer")
122
  )
123
 
 
23
  openai.api_key = "YOUR_OPENAI_API_KEY"
24
 
25
  def extract_files_from_folder(folder_path):
26
+ """Scans a folder and its subfolders for PDF, TXT, CSV, and DOCX files."""
27
+ extracted_files = {"pdf": [], "txt": [], "csv": [], "docx": []}
28
 
29
  for root, _, files in os.walk(folder_path):
30
  for file_name in files:
 
35
  extracted_files["txt"].append(file_path)
36
  elif file_name.endswith(".csv"):
37
  extracted_files["csv"].append(file_path)
38
+ elif file_name.endswith(".docx"):
39
+ extracted_files["docx"].append(file_path)
40
  return extracted_files
41
 
42
  def read_text_from_files(file_paths):
 
101
 
102
  def chatbot_interface(question):
103
  folder_path = "/mnt/data/Data Analitics/"
 
 
 
104
  extracted_files = extract_files_from_folder(folder_path)
105
 
106
  text = get_text_from_pdf(extracted_files["pdf"]) + read_text_from_files(extracted_files["txt"]) + get_text_from_csv(extracted_files["csv"])
107
 
108
  if not text:
109
+ return "The folder does not contain valid PDF, TXT, CSV, or DOCX files. Please upload supported file types."
110
 
111
  corrected_exercises = correct_exercises(text)
112
  vector_db = create_vector_database(text)
 
115
  # Gradio interface
116
  demo = gr.Interface(
117
  fn=chatbot_interface,
118
+ inputs=gr.Textbox(label="Ask a question", placeholder="Type your question here..."),
 
119
  outputs=gr.Textbox(label="Answer")
120
  )
121