Update app.py
Browse files
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.
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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)
|