Spaces:
Sleeping
Sleeping
0504ankitsharma
commited on
Update app/main.py
Browse files- app/main.py +12 -21
app/main.py
CHANGED
@@ -17,6 +17,7 @@ import time
|
|
17 |
|
18 |
# Set writable paths for cache and data
|
19 |
cache_dir = '/tmp'
|
|
|
20 |
nltk_data_path = os.path.join(cache_dir, 'nltk_data')
|
21 |
|
22 |
# Configure NLTK and other library paths
|
@@ -27,20 +28,12 @@ os.environ['XDG_CACHE_HOME'] = cache_dir
|
|
27 |
# Add NLTK data path
|
28 |
nltk.data.path.append(nltk_data_path)
|
29 |
|
30 |
-
# Ensure the
|
31 |
-
|
32 |
-
|
33 |
-
except OSError as e:
|
34 |
-
print(f"Error creating directory {nltk_data_path}: {e}")
|
35 |
-
raise
|
36 |
|
37 |
# Download required NLTK resources
|
38 |
-
|
39 |
-
nltk.download('punkt', download_dir=nltk_data_path)
|
40 |
-
print("NLTK 'punkt' resource downloaded successfully.")
|
41 |
-
except Exception as e:
|
42 |
-
print(f"Error downloading NLTK resources: {e}")
|
43 |
-
raise
|
44 |
|
45 |
def clean_response(response):
|
46 |
# Remove any leading/trailing whitespace, including newlines
|
@@ -70,7 +63,7 @@ app.add_middleware(
|
|
70 |
openai_api_key = os.environ.get('OPENAI_API_KEY')
|
71 |
llm = ChatOpenAI(
|
72 |
api_key=openai_api_key,
|
73 |
-
model_name="gpt-4-turbo-preview",
|
74 |
temperature=0.7,
|
75 |
max_tokens=200
|
76 |
)
|
@@ -85,10 +78,9 @@ class Query(BaseModel):
|
|
85 |
prompt = ChatPromptTemplate.from_template(
|
86 |
"""
|
87 |
You are a helpful assistant designed specifically for the Thapar Institute of Engineering and Technology (TIET), a renowned technical college. Your task is to answer all queries related to TIET in a concise manner. Every response you provide should be relevant to the context of TIET. If a question falls outside of this context, please decline by stating, 'Sorry, I cannot help with that.' If you do not know the answer to a question, do not attempt to fabricate a response; instead, politely decline.
|
88 |
-
but avoid sounding boastful or exaggerating. Stay focused on the context provided.
|
89 |
If the query is not related to TIET or falls outside the context of education, respond with:
|
90 |
"Sorry, I cannot help with that. I'm specifically designed to answer questions about the Thapar Institute of Engineering and Technology.
|
91 |
-
For more information, please contact
|
92 |
<context>
|
93 |
{context}
|
94 |
</context>
|
@@ -117,10 +109,11 @@ def vector_embedding():
|
|
117 |
encode_kwargs = {'normalize_embeddings': True}
|
118 |
model_norm = HuggingFaceBgeEmbeddings(model_name=model_name, encode_kwargs=encode_kwargs)
|
119 |
|
|
|
120 |
db = FAISS.from_documents(chunks, model_norm)
|
121 |
-
db.save_local(
|
122 |
|
123 |
-
print("Vector store created and saved successfully.")
|
124 |
return {"response": "Vector Store DB Is Ready"}
|
125 |
|
126 |
except Exception as e:
|
@@ -137,7 +130,7 @@ def get_embeddings():
|
|
137 |
def read_item(query: Query):
|
138 |
try:
|
139 |
embeddings = get_embeddings()
|
140 |
-
vectors = FAISS.load_local(
|
141 |
except Exception as e:
|
142 |
print(f"Error loading vector store: {str(e)}")
|
143 |
return {"response": "Vector Store Not Found or Error Loading. Please run /setup first."}
|
@@ -154,9 +147,7 @@ def read_item(query: Query):
|
|
154 |
# Apply the cleaning function to the response
|
155 |
cleaned_response = clean_response(response['answer'])
|
156 |
|
157 |
-
# For debugging, print the cleaned response
|
158 |
print("Cleaned response:", repr(cleaned_response))
|
159 |
-
|
160 |
return {"response": cleaned_response}
|
161 |
else:
|
162 |
return {"response": "No Query Found"}
|
@@ -167,4 +158,4 @@ def setup():
|
|
167 |
|
168 |
if __name__ == "__main__":
|
169 |
import uvicorn
|
170 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
|
|
17 |
|
18 |
# Set writable paths for cache and data
|
19 |
cache_dir = '/tmp'
|
20 |
+
writable_dir = os.path.join(cache_dir, 'vectors_db')
|
21 |
nltk_data_path = os.path.join(cache_dir, 'nltk_data')
|
22 |
|
23 |
# Configure NLTK and other library paths
|
|
|
28 |
# Add NLTK data path
|
29 |
nltk.data.path.append(nltk_data_path)
|
30 |
|
31 |
+
# Ensure the directories exist
|
32 |
+
os.makedirs(nltk_data_path, exist_ok=True)
|
33 |
+
os.makedirs(writable_dir, exist_ok=True)
|
|
|
|
|
|
|
34 |
|
35 |
# Download required NLTK resources
|
36 |
+
nltk.download('punkt', download_dir=nltk_data_path)
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
def clean_response(response):
|
39 |
# Remove any leading/trailing whitespace, including newlines
|
|
|
63 |
openai_api_key = os.environ.get('OPENAI_API_KEY')
|
64 |
llm = ChatOpenAI(
|
65 |
api_key=openai_api_key,
|
66 |
+
model_name="gpt-4-turbo-preview",
|
67 |
temperature=0.7,
|
68 |
max_tokens=200
|
69 |
)
|
|
|
78 |
prompt = ChatPromptTemplate.from_template(
|
79 |
"""
|
80 |
You are a helpful assistant designed specifically for the Thapar Institute of Engineering and Technology (TIET), a renowned technical college. Your task is to answer all queries related to TIET in a concise manner. Every response you provide should be relevant to the context of TIET. If a question falls outside of this context, please decline by stating, 'Sorry, I cannot help with that.' If you do not know the answer to a question, do not attempt to fabricate a response; instead, politely decline.
|
|
|
81 |
If the query is not related to TIET or falls outside the context of education, respond with:
|
82 |
"Sorry, I cannot help with that. I'm specifically designed to answer questions about the Thapar Institute of Engineering and Technology.
|
83 |
+
For more information, please contact our toll-free number: 18002024100 or E-mail us at admissions@thapar.edu
|
84 |
<context>
|
85 |
{context}
|
86 |
</context>
|
|
|
109 |
encode_kwargs = {'normalize_embeddings': True}
|
110 |
model_norm = HuggingFaceBgeEmbeddings(model_name=model_name, encode_kwargs=encode_kwargs)
|
111 |
|
112 |
+
# Save FAISS vector store to a writable directory
|
113 |
db = FAISS.from_documents(chunks, model_norm)
|
114 |
+
db.save_local(writable_dir)
|
115 |
|
116 |
+
print(f"Vector store created and saved successfully to {writable_dir}.")
|
117 |
return {"response": "Vector Store DB Is Ready"}
|
118 |
|
119 |
except Exception as e:
|
|
|
130 |
def read_item(query: Query):
|
131 |
try:
|
132 |
embeddings = get_embeddings()
|
133 |
+
vectors = FAISS.load_local(writable_dir, embeddings, allow_dangerous_deserialization=True)
|
134 |
except Exception as e:
|
135 |
print(f"Error loading vector store: {str(e)}")
|
136 |
return {"response": "Vector Store Not Found or Error Loading. Please run /setup first."}
|
|
|
147 |
# Apply the cleaning function to the response
|
148 |
cleaned_response = clean_response(response['answer'])
|
149 |
|
|
|
150 |
print("Cleaned response:", repr(cleaned_response))
|
|
|
151 |
return {"response": cleaned_response}
|
152 |
else:
|
153 |
return {"response": "No Query Found"}
|
|
|
158 |
|
159 |
if __name__ == "__main__":
|
160 |
import uvicorn
|
161 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|