Rohitface commited on
Commit
140fca2
·
verified ·
1 Parent(s): b2a7d5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  import chromadb
3
  from sentence_transformers import SentenceTransformer
4
- import pandas as pd # Make sure pandas is imported
5
 
6
  # --- 1. Load Model (No changes here) ---
7
  print("Loading sentence-transformer model...")
@@ -15,23 +14,23 @@ try:
15
  collection = client.create_collection("my_documents")
16
  print("ChromaDB collection created.")
17
 
18
- # --- THIS IS THE UPDATED SECTION ---
19
- # Load your data from the CSV file.
20
- # IMPORTANT: Change 'text' to the actual name of the column in your CSV that contains the text data.
21
  try:
22
- print("Loading data from my_data.csv...")
23
- df = pd.read_csv('my_data.csv')
24
- # Ensure you change 'text' to your column's name if it's different
25
- documents = df['text'].tolist()
26
- print(f"Successfully loaded {len(documents)} documents.")
27
- except FileNotFoundError:
28
- print("Error: my_data.csv not found. Please upload the file to your Space.")
29
- # Create a fallback document to avoid a crash
30
- documents = ["Error: my_data.csv not found. Please make sure the file is uploaded to the Hugging Face Space."]
31
- except KeyError:
32
- print("Error: The CSV must have a column named 'text'. Please rename your column or update the code.")
33
- documents = ["Error: Could not find a 'text' column in the CSV file. Please check your data."]
34
 
 
 
 
35
  # --- END OF UPDATED SECTION ---
36
 
37
  embeddings = model.encode(documents)
 
1
  import gradio as gr
2
  import chromadb
3
  from sentence_transformers import SentenceTransformer
 
4
 
5
  # --- 1. Load Model (No changes here) ---
6
  print("Loading sentence-transformer model...")
 
14
  collection = client.create_collection("my_documents")
15
  print("ChromaDB collection created.")
16
 
17
+ # --- THIS IS THE UPDATED SECTION FOR READING .TXT ---
 
 
18
  try:
19
+ print("Loading data from my_data.txt...")
20
+ with open('my_data.txt', 'r', encoding='utf-8') as f:
21
+ # Each line in the .txt file becomes a separate document.
22
+ # It also removes any empty lines.
23
+ documents = [line.strip() for line in f if line.strip()]
24
+
25
+ if not documents:
26
+ print("Warning: my_data.txt is empty or contains only whitespace.")
27
+ documents = ["Error: The data file 'my_data.txt' appears to be empty."]
28
+ else:
29
+ print(f"Successfully loaded {len(documents)} lines from my_data.txt.")
 
30
 
31
+ except FileNotFoundError:
32
+ print("Error: my_data.txt not found. Please upload the file to your Space.")
33
+ documents = ["Error: my_data.txt not found. Please make sure the file is uploaded."]
34
  # --- END OF UPDATED SECTION ---
35
 
36
  embeddings = model.encode(documents)