Spaces:
Runtime error
Runtime error
Srinivasulu kethanaboina
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,18 @@ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
import csv
|
9 |
import os
|
|
|
|
|
|
|
10 |
|
11 |
PERSIST_DIR = "history" # Replace with your actual directory path
|
12 |
CSV_FILE = os.path.join(PERSIST_DIR, "chat_history.csv")
|
13 |
|
|
|
|
|
|
|
|
|
|
|
14 |
# Load environment variables
|
15 |
load_dotenv()
|
16 |
|
@@ -46,6 +54,8 @@ def data_ingestion_from_directory():
|
|
46 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
47 |
|
48 |
def handle_query(query):
|
|
|
|
|
49 |
# Ensure the directory exists or create it
|
50 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
51 |
|
@@ -68,9 +78,9 @@ def handle_query(query):
|
|
68 |
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
69 |
index = load_index_from_storage(storage_context)
|
70 |
|
71 |
-
# Use chat history to enhance response
|
72 |
context_str = ""
|
73 |
-
for past_query, response in reversed(current_chat_history):
|
74 |
if past_query.strip():
|
75 |
context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
|
76 |
|
@@ -85,7 +95,8 @@ def handle_query(query):
|
|
85 |
response = "Sorry, I couldn't find an answer."
|
86 |
|
87 |
# Update current chat history
|
88 |
-
current_chat_history.append(
|
|
|
89 |
|
90 |
# Save chat history to CSV
|
91 |
with open(CSV_FILE, 'a', newline='', encoding='utf-8') as file:
|
|
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
import csv
|
9 |
import os
|
10 |
+
import csv
|
11 |
+
import os
|
12 |
+
from datasets import Dataset, DatasetDict
|
13 |
|
14 |
PERSIST_DIR = "history" # Replace with your actual directory path
|
15 |
CSV_FILE = os.path.join(PERSIST_DIR, "chat_history.csv")
|
16 |
|
17 |
+
# Assuming current_chat_history is managed within a Dataset or DatasetDict
|
18 |
+
current_chat_history = Dataset({"query": [], "response": []})
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
# Load environment variables
|
23 |
load_dotenv()
|
24 |
|
|
|
54 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
55 |
|
56 |
def handle_query(query):
|
57 |
+
global current_chat_history
|
58 |
+
|
59 |
# Ensure the directory exists or create it
|
60 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
61 |
|
|
|
78 |
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
79 |
index = load_index_from_storage(storage_context)
|
80 |
|
81 |
+
# Use chat history to enhance response
|
82 |
context_str = ""
|
83 |
+
for past_query, response in reversed(current_chat_history["query"]):
|
84 |
if past_query.strip():
|
85 |
context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
|
86 |
|
|
|
95 |
response = "Sorry, I couldn't find an answer."
|
96 |
|
97 |
# Update current chat history
|
98 |
+
current_chat_history["query"].append(query)
|
99 |
+
current_chat_history["response"].append(response)
|
100 |
|
101 |
# Save chat history to CSV
|
102 |
with open(CSV_FILE, 'a', newline='', encoding='utf-8') as file:
|