Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from flask import Flask, render_template, request, jsonify
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
import random
|
@@ -8,9 +8,11 @@ keys = [
|
|
8 |
os.getenv("KEY2"),
|
9 |
os.getenv("KEY3"),
|
10 |
os.getenv("KEY4"),
|
11 |
-
os.getenv("KEY5")
|
|
|
12 |
|
13 |
app = Flask(__name__)
|
|
|
14 |
|
15 |
client = InferenceClient(
|
16 |
"meta-llama/Meta-Llama-3-8B-Instruct",
|
@@ -25,8 +27,15 @@ def home():
|
|
25 |
def chat():
|
26 |
user_message = request.json['message']
|
27 |
|
28 |
-
#
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
What are the primary responsibilities of RAC?
|
32 |
|
@@ -178,12 +187,21 @@ You can apply for summer training to the Director of a suitable
|
|
178 |
|
179 |
DRDO laboratory or institution. The procedure typically involves submitting an application detailing your academic background, research interests, and proposed project.
|
180 |
</file>
|
181 |
-
|
182 |
-
|
183 |
-
<Tannu>"
|
184 |
|
185 |
response = client.text_generation(prompt, max_new_tokens=1024, stop_sequences=["<user>"])
|
186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
return jsonify({'message': response})
|
188 |
|
189 |
if __name__ == '__main__':
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify, session
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
import random
|
|
|
8 |
os.getenv("KEY2"),
|
9 |
os.getenv("KEY3"),
|
10 |
os.getenv("KEY4"),
|
11 |
+
os.getenv("KEY5")
|
12 |
+
]
|
13 |
|
14 |
app = Flask(__name__)
|
15 |
+
app.secret_key = os.urandom(24) # Set a secret key for session management
|
16 |
|
17 |
client = InferenceClient(
|
18 |
"meta-llama/Meta-Llama-3-8B-Instruct",
|
|
|
27 |
def chat():
|
28 |
user_message = request.json['message']
|
29 |
|
30 |
+
# Initialize or retrieve the conversation history
|
31 |
+
if 'history' not in session:
|
32 |
+
session['history'] = []
|
33 |
+
|
34 |
+
# Add the user's message to the history
|
35 |
+
session['history'].append(f"<user>{user_message}</user>")
|
36 |
+
|
37 |
+
# Construct the prompt with the entire conversation history
|
38 |
+
prompt = """<file>Frequently Asked Questions (FAQs)
|
39 |
|
40 |
What are the primary responsibilities of RAC?
|
41 |
|
|
|
187 |
|
188 |
DRDO laboratory or institution. The procedure typically involves submitting an application detailing your academic background, research interests, and proposed project.
|
189 |
</file>
|
190 |
+
system>Act as a DRDO FAQ chatbot named Tannu. Reply to the user's queries based on the FAQ document.</system>\n"""
|
191 |
+
prompt += "\n".join(session['history'])
|
192 |
+
prompt += "\n<Tannu>"
|
193 |
|
194 |
response = client.text_generation(prompt, max_new_tokens=1024, stop_sequences=["<user>"])
|
195 |
|
196 |
+
# Add the bot's response to the history
|
197 |
+
session['history'].append(f"<Tannu>{response}</Tannu>")
|
198 |
+
|
199 |
+
# Trim history if it gets too long (optional, adjust the number as needed)
|
200 |
+
if len(session['history']) > 10:
|
201 |
+
session['history'] = session['history'][-10:]
|
202 |
+
|
203 |
+
session.modified = True # Ensure the session is saved
|
204 |
+
|
205 |
return jsonify({'message': response})
|
206 |
|
207 |
if __name__ == '__main__':
|