-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1591 lines (1275 loc) · 57.7 KB
/
app.py
File metadata and controls
1591 lines (1275 loc) · 57.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, render_template, request
from langchain.chains import ConversationalRetrievalChain
from langchain.prompts import PromptTemplate
from langchain_openai import AzureChatOpenAI, AzureOpenAIEmbeddings
from langchain_pinecone import Pinecone as PineconeVectorStore
import os
from pinecone import Pinecone
import time
import re
from pinecone import ServerlessSpec
from flask import session
import uuid
from flask import Flask, Blueprint, request, jsonify, send_from_directory, render_template
from werkzeug.utils import secure_filename
from datetime import datetime, timezone
from pinecone import Pinecone, ServerlessSpec
from langchain_openai import AzureOpenAIEmbeddings
from langchain.document_loaders import PyPDFLoader, TextLoader
from langchain_openai import AzureChatOpenAI
from azure.cosmos import CosmosClient
from azure.identity import DefaultAzureCredential
from azure.cosmos.aio import CosmosClient
import uuid, time, json, requests, os
from flask import Flask, render_template, url_for, request, redirect, session
from langchain.text_splitter import RecursiveCharacterTextSplitter
import os
from flask import Flask, jsonify
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
generated_buttons = []
# Initialize Pinecone client
pc = Pinecone(api_key=os.environ.get('PINECONE_API_KEY'))
# Initialize RAG-related components
embeddings = AzureOpenAIEmbeddings(
deployment="VARELab-TxtEmbeddingLarge",
model="text-embedding-3-large",
#api_key=os.getenv("OPENAI_API_KEY"),
api_key=os.environ.get('AZURE_OPENAI_VARE_KEY'),
openai_api_version="2023-05-15",
azure_endpoint=os.environ.get('AZURE_ENDPOINT'),
openai_api_type="azure",
chunk_size=512
)
index_name = "langchain-test-index"
existing_indexes = [index_info["name"] for index_info in pc.list_indexes()]
if index_name not in existing_indexes:
pc.create_index(
name=index_name,
dimension=3072,
metric="cosine",
spec=ServerlessSpec(cloud="aws", region="us-east-1")
)
while not pc.describe_index(index_name).status["ready"]:
time.sleep(1)
index = pc.Index(index_name)
vectorstore = PineconeVectorStore(index=index, embedding=embeddings, text_key="text")
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 2})
index_name_Q = "ivory-q-index"
existing_indexes_Q = [index_info["name"] for index_info in pc.list_indexes()]
if index_name_Q not in existing_indexes_Q:
pc.create_index(
name=index_name,
dimension=3072,
metric="cosine",
spec=ServerlessSpec(cloud="aws", region="us-east-1")
)
while not pc.describe_index(index_name).status["ready"]:
time.sleep(1)
# Initialize the Pinecone Vector Store
index_Q = pc.Index(index_name_Q)
vectorstore_Q = PineconeVectorStore(index=index_Q, embedding=embeddings, text_key="text")
retriever_Q = vectorstore_Q.as_retriever(search_type="similarity", search_kwargs={"k": 3})
# Initialize the primary LLM for answering the user's query
LLM_Primary = AzureChatOpenAI(
azure_deployment="VARELab-GPT4o",
api_key=os.environ.get('AZURE_OPENAI_VARE_KEY'),
api_version="2024-08-01-preview",
azure_endpoint=os.environ.get('AZURE_ENDPOINT'),
temperature=0.5,
max_tokens=None,
timeout=None,
max_retries=2,
)
# Create the condense prompt for the primary LLM
CONDENSE_PROMPT_PRIMARY = PromptTemplate.from_template("""
You are an assistant helping to condense a follow-up question in a conversation between a parent and an Avatar (doctor) about the child's dental hygiene. Use the chat history to rephrase the parent's follow-up question into a standalone question that includes references to any people mentioned.
Chat History:
{chat_history}
Follow-Up Input: {question}
Standalone Question:
""")
# Create the QA prompt for the primary LLM
QA_PROMPT_PRIMARY = PromptTemplate.from_template("""
You are an Avatar (doctor) speaking to a parent about their child's dental hygiene. You must strictly adhere to the script provided below. Use only the information and responses from the script. When appropriate, you can use exact lines from the script, but you may also paraphrase to maintain clarity and coherence. Use the chat history to understand who you are talking to and refer to the individuals appropriately. Provide direct answers to the parent's questions and comments, and talk like a medical professional. Do not include any information not present in the script. Do not ask any follow-up questions unless the parent explicitly asks for more information.
Script Details:
{context}
Conversation History:
{chat_history}
Parent's Question: {question}
Avatar's Response:
""")
# Initialize the Conversational Retrieval Chain for the primary LLM
qa_chain_primary = ConversationalRetrievalChain.from_llm(
llm=LLM_Primary,
retriever=retriever,
condense_question_prompt=CONDENSE_PROMPT_PRIMARY,
combine_docs_chain_kwargs={'prompt': QA_PROMPT_PRIMARY},
return_source_documents=True,
verbose=False
)
# Example usage
chat_history = []
# Session-specific storage
chat_histories = {}
active_sessions = {} # Will store chat histories and follow-ups per session ID
pending_follow_ups = {} # Store follow-up questions per session
def clean_questions(questions):
"""
Removes leading numbers and other non-alphabetic characters from each question.
Args:
- questions (list of str): List of questions with potential leading numbers.
Returns:
- list of str: Cleaned list of questions without leading numbers or symbols.
"""
cleaned_questions = [re.sub(r'^\d+[\.\)]?\s*', '', question) for question in questions]
return cleaned_questions
from flask import Flask, render_template, request, jsonify, send_from_directory, redirect, url_for
# Azure Speech Configuration
SPEECH_KEY = os.environ.get('AZURE_SPEECH_KEY')
SERVICE_REGION = "eastus"
VOICE_NAME = "drdavidNeural"
# Path to store synthesized audio files
STATIC_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
os.makedirs(STATIC_FOLDER, exist_ok=True)
app = Flask(__name__)
app.secret_key = os.urandom(32) # Secret key for session management
##############################################################
# COSMOS DB CONTENT
import re
from langchain.schema import BaseRetriever, Document
from langchain.chains import ConversationalRetrievalChain
from langchain_openai import AzureChatOpenAI
from langchain.prompts import PromptTemplate
from typing import List, Callable
import asyncio
from pydantic import BaseModel, Field
from pydantic.functional_validators import SkipValidation
# Custom Retriever Class for Cosmos DB
class CosmosDBRetriever(BaseRetriever, BaseModel):
search_function: SkipValidation[Callable] = Field(...)
category_id: str = Field(...)
class Config:
arbitrary_types_allowed = True
def _get_relevant_documents(self, query: str) -> List[Document]:
# Run the coroutine in a new event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
# Run the async function and get results
results = loop.run_until_complete(
self.search_function(query, self.category_id)
)
finally:
loop.close()
documents = []
for result in results:
# Get the metadata directly from the document
metadata = result.get('metadata', {})
# Ensure we have the core metadata fields from Cosmos DB
if metadata:
# Add any additional metadata from the result
metadata['source_type'] = result.get('source_type', 'unknown')
metadata['score'] = result.get('similarity', 0)
metadata['category_id'] = result.get('category_id', self.category_id)
# Print the metadata for debugging
print(f"[DEBUG] Document metadata before passing to Document: {metadata}")
# Create document with metadata
doc = Document(
page_content=result['text'],
metadata=metadata
)
else:
# Create a basic document with minimal metadata if none exists
doc = Document(
page_content=result['text'],
metadata={
'source': result.get('source_type', 'unknown'),
'score': result.get('similarity', 0),
'category_id': result.get('category_id', self.category_id)
}
)
documents.append(doc)
return documents
from typing import Dict, Tuple, Optional
import asyncio
from datetime import datetime
from langchain.chains import ConversationalRetrievalChain
from langchain.prompts import PromptTemplate
# Global session storage
session_data = {}
class ChatSession:
def __init__(self, session_id: str, category_id: str):
self.session_id = session_id
self.category_id = category_id
self.chat_history = []
self.qa_chain = None
self.qa_prompt = None
self.initialized = False
@app.route('/initialize_chat', methods=['POST'])
def initialize_chat():
# Clear any existing session data for this user
if 'session_id' in session:
session_id = session['session_id']
if session_id in session_data:
del session_data[session_id]
# Create new session ID
session['session_id'] = str(uuid.uuid4())
data = request.get_json()
category_id = data.get('categoryId')
if category_id:
try:
# Initialize new chat session
initialize_chat_session(session['session_id'], category_id)
return jsonify({"status": "success", "session_id": session['session_id']})
except Exception as e:
import traceback
error_traceback = traceback.format_exc()
print(f"[DETAILED ERROR] {error_traceback}")
return jsonify({"error": str(e)}), 500
return jsonify({"error": "No category ID provided"}), 400
def initialize_chat_session(session_id: str, category_id: str) -> ChatSession:
"""Initialize a new chat session with necessary components"""
if session_id in session_data:
return session_data[session_id]
try:
session = ChatSession(session_id, category_id)
# Query Pinecone for the QA prompt
custom_index_name = "custom-rag-vare"
pinecone_api_key = os.environ.get('PINECONE_API_KEY')
pc = Pinecone(api_key=pinecone_api_key)
# Get the index dimension first
index_info = pc.describe_index(custom_index_name)
index_dimension = index_info.dimension
print(f"[DEBUG] Index dimension is {index_dimension}")
# Create the appropriate zero vector
zero_vector = [0] * index_dimension
# Query all vectors with the correct dimension
index = pc.Index(custom_index_name)
response = index.query(
vector=zero_vector,
top_k=100,
include_metadata=True
)
# Find the matching record
matching_prompt = None
for match in response['matches']:
try:
metadata_text = match['metadata']['text']
metadata_dict = eval(metadata_text)
if metadata_dict.get('index_name') == category_id:
matching_prompt = metadata_dict['prompts']['qa_prompt']
break
except Exception as e:
print(f"Error processing match: {e}")
continue
# Create default prompt if none found or if the found prompt doesn't have required variables
default_prompt = """Use the following context to answer the question. If you cannot answer from the context, say you don't have enough information. Answer in 2-4 sentences only. No long answers.
Context:
{context}
Chat History:
{chat_history}
Question:
{question}
Answer:"""
intro_prompt = "You are a virtual avatar, designed to help provide insightful information and answer queries formally and in an inviting nature.\n\n"
if matching_prompt:
# Verify the prompt has all required variables
if all(var in matching_prompt for var in ["{context}", "{chat_history}", "{question}"]):
# Append the sentence limit instruction to the prompt
qa_prompt_template = intro_prompt + matching_prompt + "\n\n Answer in 2-3 sentences only. No long answers."
else:
print(f"WARNING: Prompt for category {category_id} missing required variables. Using default prompt.")
qa_prompt_template = intro_prompt + matching_prompt + default_prompt
else:
qa_prompt_template = default_prompt
if matching_prompt:
# Create two separate prompts - one for condensing and one for QA
condense_prompt = PromptTemplate.from_template("""Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:""")
qa_prompt = PromptTemplate(
template=qa_prompt_template,
# Make sure all these variables are available in the template
input_variables=["context", "chat_history", "question"]
)
else:
raise ValueError(f"No matching prompts found for category_id: {category_id}")
# Initialize components
cosmos_retriever = CosmosDBRetriever(
search_function=similarity_search_by_category,
category_id=category_id
)
llm = AzureChatOpenAI(
azure_deployment="VARELab-GPT4o",
api_key=os.environ.get('AZURE_OPENAI_VARE_KEY'),
api_version="2024-08-01-preview",
azure_endpoint=os.environ.get('AZURE_ENDPOINT'),
temperature=0.5
)
# Modified QA chain initialization
session.qa_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=cosmos_retriever,
condense_question_prompt=condense_prompt, # Use separate condense prompt
combine_docs_chain_kwargs={
'prompt': qa_prompt,
'document_variable_name': 'context' # Explicitly set this
},
return_source_documents=True,
verbose=True
)
session.initialized = True
session_data[session_id] = session
return session
except Exception as e:
print(f"Error initializing chat session: {str(e)}")
raise
def process_chat(session_id: str, category_id: str, user_query: str):
"""Process a chat message and return response"""
try:
# Simply get the existing session
if session_id not in session_data:
raise ValueError("Chat session not initialized. Please start a new conversation.")
chat_session = session_data[session_id]
# Process query
result = chat_session.qa_chain({
"question": user_query,
"chat_history": chat_session.chat_history
})
response = result['answer']
source_documents = result.get("source_documents", [])
# Debug output
print(f"[DEBUG] Retrieved {len(source_documents)} source documents")
# Dictionary to consolidate sources by document
document_pages = {}
# Process source documents to extract metadata
for i, doc in enumerate(source_documents):
print(f"\n[DEBUG] Processing document {i+1}:")
if hasattr(doc, 'metadata'):
metadata = doc.metadata
# Extract filename
filename = None
if 'source_filename' in metadata:
filename = metadata['source_filename']
elif 'source' in metadata and metadata['source'] != 'unknown':
if isinstance(metadata['source'], str) and ('/' in metadata['source'] or '\\' in metadata['source']):
filename = os.path.basename(metadata['source'])
else:
filename = metadata['source']
# Skip if no valid filename
if not filename or filename == "Unknown Source":
continue
# Extract page number
page_num = None
if 'page_number' in metadata:
page_num = metadata['page_number']
elif 'page' in metadata:
page_num = int(metadata['page']) + 1
# Add to document_pages dictionary
if filename not in document_pages:
document_pages[filename] = set()
if page_num:
document_pages[filename].add(page_num)
# Construct HTML citation with hover effect
if document_pages:
citation_html = '<span class="citation-container">[Source]<span class="citation-hover">'
# Add each document with its pages
for filename, pages in document_pages.items():
if pages:
# Sort page numbers
sorted_pages = sorted(pages)
page_str = "Page" if len(sorted_pages) == 1 else "Pages"
pages_formatted = ", ".join(str(p) for p in sorted_pages)
citation_html += f"{filename} - {page_str} {pages_formatted}<br>"
else:
citation_html += f"{filename}<br>"
citation_html += '</span></span>'
# Add CSS for hover effect inline (will be included in the response)
css_style = """
<style>
.citation-container {
position: relative;
color: blue;
text-decoration: underline;
cursor: pointer;
display: inline-block;
}
.citation-hover {
visibility: hidden;
position: absolute;
z-index: 1000;
bottom: 125%;
left: 0;
background-color: #333;
color: #fff;
text-align: left;
border-radius: 6px;
padding: 10px;
opacity: 0;
transition: opacity 0.3s, visibility 0.3s;
/* Ensure full content display */
white-space: normal;
width: max-content;
max-width: 300px;
word-wrap: break-word;
font-weight: normal;
font-size: 14px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.citation-container:hover .citation-hover {
visibility: visible;
opacity: 1;
}
</style>
"""
# Add the citation to the response
response += " " + css_style + citation_html
# Update chat history with the formatted response
chat_session.chat_history.append((user_query, response))
# Return response and updated chat history
cleaned_response = re.sub(r'^Avatar:\s*', '', response)
return cleaned_response, chat_session.chat_history
except Exception as e:
print(f"Error in chat processing: {str(e)}")
import traceback
traceback.print_exc()
return f"I apologize, but I encountered an error: {str(e)}", []
@app.route('/main2', methods=['GET', 'POST'])
def main_page2():
if 'session_id' not in session:
session['session_id'] = str(uuid.uuid4())
session_id = session['session_id']
if request.method == 'POST':
data = request.get_json()
user_query = data.get('user_query')
category_id = data.get('categoryId')
if user_query and category_id:
response, chat_history = process_chat(
session_id=session_id,
category_id=category_id,
user_query=user_query
)
return jsonify({
"response": response,
"chat_history": chat_history
})
return render_template('index.html',
session_id=session_id,
response=None,
chat_history=[])
#################################################
# upload.py content
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER # Ensure Flask app config has this value
# Configuration
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'pdf', 'txt'}
MAX_CONTENT_LENGTH = 16 * 1024 * 1024 # 16MB max file size
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Azure OpenAI Configuration
AZURE_OPENAI_API_KEY = os.environ.get('AZURE_OPENAI_VARE_KEY')
AZURE_API_BASE = os.environ.get('AZURE_ENDPOINT')
AZURE_API_VERSION = "2023-10-01-preview"
# Pinecone Configuration
PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY2')
HOST = os.environ.get('COSMOS_HOST')
KEY = os.environ.get('COSMOS_KEY')
cosmos_client = CosmosClient(HOST, KEY)
database_name = "varelab-website"
container_name = "ivory-draft"
llm = AzureChatOpenAI(
azure_deployment="VARELab-GPT4o",
api_key=os.environ.get('AZURE_OPENAI_VARE_KEY'),
azure_endpoint=os.environ.get('AZURE_ENDPOINT'),
api_version="2024-08-01-preview",
temperature=0.5,
max_tokens=None,
timeout=None,
max_retries=2,
)
# Your existing Azure OpenAI embeddings setup
azure_embeddings = AzureOpenAIEmbeddings(
deployment="VARELab-TxtEmbeddingLarge",
model="text-embedding-3-large",
api_key=os.environ.get('AZURE_OPENAI_VARE_KEY'),
azure_endpoint=os.environ.get('AZURE_ENDPOINT'),
openai_api_version="2023-05-15",
openai_api_type="azure",
chunk_size=512
)
###########################################################################################
@app.route('/index')
def index_page():
session['session_id'] = str(uuid.uuid4()) # Always assign a new session ID
return render_template('index.html', session_id=session['session_id'])
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/static/<path:filename>')
def serve_audio(filename):
return send_from_directory(STATIC_FOLDER, filename, mimetype='audio/mpeg')
def cleanup_old_sessions(max_age=3600): # max_age in seconds (1 hour)
current_time = time.time()
for session_id in list(chat_histories.keys()):
if current_time - session.get(f'last_access_{session_id}', 0) > max_age:
del chat_histories[session_id]
del pending_follow_ups[session_id] # Also clean up follow-ups
# Schedule periodic cleanup (you can call this periodically using a scheduler)
def periodic_cleanup():
cleanup_old_sessions()
@app.route('/')
def index():
return render_template('avatar-page.html')
@app.route('/start', methods=['GET', 'POST'])
def start_page():
if request.method == 'POST':
user_id = request.form.get('user_id') or request.args.get('user_id', "unknown")
session['user_id'] = user_id
session['session_id'] = str(uuid.uuid4())
return redirect(url_for('main_page', session_id=session['session_id'], user_id=session['user_id']))
user_id = request.args.get('user_id')
return render_template('start.html', session_id=session.get('session_id', ''), user_id=user_id)
@app.route('/main', methods=['GET', 'POST'])
def main_page():
# Get or create session ID
if 'session_id' not in session:
session['session_id'] = str(uuid.uuid4())
print(f"[DEBUG] New session created: {session['session_id']}")
session_id = session['session_id']
user_id = session.get('user_id', 'unknown')
# Debugging
print(f"Session ID: {session_id}")
print(f"User ID: {user_id}")
# Initialize if not exists
if session_id not in chat_histories:
chat_histories[session_id] = []
pending_follow_ups[session_id] = []
if request.method == 'POST':
data = request.get_json()
user_query = data.get('user_query') if data else None
if user_query:
try:
# Process user query with session-specific chat history
result_primary = qa_chain_primary.apply([{
"question": user_query,
"chat_history": chat_histories[session_id]
}])[0]
response = result_primary['answer']
chat_histories[session_id].append((user_query, response))
cleaned_response = re.sub(r'^Avatar:\s*', '', response)
# Generate follow-up questions for this session
top_chunks = retriever_Q.get_relevant_documents(response)
follow_up_questions = clean_questions([chunk.page_content for chunk in top_chunks])
# Save follow-up questions for this session
pending_follow_ups[session_id] = follow_up_questions
print(f"Follow-Up Questions Updated for session {session_id}:", follow_up_questions)
# Update last access time for session cleanup
session[f'last_access_{session_id}'] = time.time()
return jsonify({"response": cleaned_response})
except Exception as e:
print(f"Error processing query: {e}")
return jsonify({"error": str(e)}), 500
return jsonify({"error": "No user query provided"}), 400
# For GET requests, pass all required parameters including session_id
return render_template('index.html',
session_id=session_id,
user_id=user_id, # Add this line
response=None,
follow_up_questions=pending_follow_ups.get(session_id, []),
chat_history=chat_histories.get(session_id, []))
# Remove the separate index route as it's not needed
# The main_page route now handles everything we need
@app.route('/get_follow_ups', methods=['GET'])
def get_follow_up_questions():
"""Serve follow-up questions for the current session."""
session_id = session.get('session_id')
if not session_id:
return jsonify({"follow_up_questions": []})
return jsonify({
"follow_up_questions": pending_follow_ups.get(session_id, [])
})
##############################################################################
# Upload.py content
# Add route to serve files from parent directory
@app.route('/sdk/<path:filename>')
def serve_sdk_files(filename):
parent_dir = os.path.dirname(os.path.abspath(__file__)) # Gets the directory where upload.py is
return send_from_directory(parent_dir, filename)
@app.route('/static/<path:filename>')
def serve_static(filename):
return send_from_directory(app.static_folder, filename)
def process_file(file_path):
"""Process a single file and return its contents and metadata"""
try:
if file_path.endswith('.pdf'):
loader = PyPDFLoader(file_path)
else: # txt file
loader = TextLoader(file_path)
documents = loader.load()
total_text = ' '.join([doc.page_content for doc in documents])
# Approximate token count (rough estimation: 1 token ≈ 4 characters)
token_count = len(total_text) / 4
# Split into chunks
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
)
chunks = text_splitter.split_documents(documents)
return {
'chunks': chunks,
'token_count': token_count,
'page_count': len(documents),
'success': True
}
except Exception as e:
print(f"[ERROR] Failed to process file {file_path}: {str(e)}")
return {
'success': False,
'error': str(e)
}
@app.route('/generate_prompt', methods=['POST'])
def generate_prompt():
try:
# Ensure files are uploaded
if 'files' not in request.files:
return jsonify({'error': 'No files provided'}), 400
# Check prompt type
prompt_type = request.form.get('prompt_type')
if not prompt_type:
return jsonify({'error': 'Prompt type not specified'}), 400
# Process uploaded files to extract content
files = request.files.getlist('files')
file_contents = []
temp_files = [] # Keep track of temporary files
try:
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
temp_files.append(file_path)
# Process the file
result = process_file(file_path)
if not result.get('success', False):
raise Exception(f"Failed to process {filename}: {result.get('error', 'Unknown error')}")
if 'chunks' in result:
for chunk in result['chunks']:
file_contents.append(chunk.page_content)
else:
print(f"Warning: No chunks found in result for {filename}")
continue
if not file_contents:
raise Exception("No valid content extracted from files")
# Summarize content for prompt generation
content_samples = [content[:1000] for content in file_contents[:3]]
content_summary = "\n\n---\n\n".join(content_samples)
# Define prompt generation instructions
messages = [
{
"role": "system",
"content": """You are an expert at crafting prompts for conversational AI.
Based on the provided content, generate a QA prompt template for a chat assistant.
The assistant acts as an avatar whose knowledge base comes exclusively from the content.
The QA prompt must guide the assistant to provide clear, concise, and accurate answers
while staying strictly within the knowledge base. Include placeholders for {context},
{chat_history}, and {question}."""
},
{
"role": "user",
"content": f"Content sample:\n{content_summary}\n\nGenerate a QA prompt for the assistant."
}
]
# Generate the prompt using the LLM
response = llm.invoke(messages)
if not response or not hasattr(response, 'content'):
raise Exception("Failed to generate prompt from LLM")
return jsonify({'prompt': response.content})
finally:
# Clean up temporary files
for temp_file in temp_files:
if os.path.exists(temp_file):
os.remove(temp_file)
except Exception as e:
print(f"[ERROR] Generate prompt failed: {str(e)}")
return jsonify({'error': str(e)}), 500
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def create_pinecone_index(index_name):
#pc = Pinecone(api_key=PINECONE_API_KEY)
pinecone_api_key = os.environ.get('PINECONE_API_KEY2')
pc = Pinecone(api_key=pinecone_api_key)
existing_indexes = [index_info["name"] for index_info in pc.list_indexes()]
if index_name not in existing_indexes:
pc.create_index(
name=index_name,
dimension=3072,
metric="cosine",
spec=ServerlessSpec(cloud="aws", region="us-east-1")
)
# Wait for index to be ready
while not pc.describe_index(index_name).status["ready"]:
time.sleep(1)
return pc.Index(index_name)
@app.route('/estimate_hosting_cost', methods=['POST'])
def estimate_hosting_cost():
if 'file' not in request.files:
return jsonify({'error': 'No file provided'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
# Save file temporarily
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
try:
result = process_file(filepath)
# Calculate cost: 50 cents per 10 pages
cost = (result['page_count'] / 10) * 0.50
return jsonify({
'hosting_cost': round(cost, 2),
'token_count': int(result['token_count']),
'page_count': result['page_count']
})
finally:
if os.path.exists(filepath):
os.remove(filepath)
@app.route('/avatar-conv.html')
def avatar_conversation():
return render_template('avatar-conv.html')
@app.route('/generate_avatar_response', methods=['POST'])
def generate_avatar_response():
data = request.json
# Get the corresponding voice based on avatar version
voice_mapping = {
'max-business': 'en-US-JacobNeural',
'lisa-casual': 'en-US-NancyNeural',
# Add other mappings
}
voice = voice_mapping.get(data['avatarVersion'], 'en-US-JacobNeural')
# Create a unique job ID
job_id = str(uuid.uuid4())
try:
# Initialize avatar synthesis
synthesis_config = {
"synthesisConfig": {
"voice": voice,
},
"avatarConfig": {
"customized": False,
"talkingAvatarCharacter": data['avatarVersion'],
"talkingAvatarStyle": 'business',
"videoFormat": "mp4",
"videoCodec": "h264",
"subtitleType": "soft_embedded",
"backgroundColor": "#FFFFFF1A",
}
}
# For now, return a simple response
return jsonify({
'success': True,
'message': 'Response generated',
'synthesis_config': synthesis_config
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/initialize_avatar', methods=['POST'])
def initialize_avatar():
try:
data = request.json
avatar_version = data.get('avatarVersion')
initial_text = data.get('text', "Hello! I'm your avatar assistant. How can I help you today?")
# Configuration for Azure Speech Service
SPEECH_ENDPOINT = os.environ.get('SPEECH_ENDPOINT')
SUBSCRIPTION_KEY = os.environ.get('SPEECH_ENDPOINT')
API_VERSION = "2024-04-15-preview"
# Get voice based on avatar version
voice_mapping = {
'max-business': 'en-US-JacobNeural',
'lisa-casual': 'en-US-NancyNeural',
'dr-david-avenetti': 'drdavidNeural',
'prof-zalake': 'en-US-JacobNeural'