Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,6 +32,7 @@ from pathlib import Path
|
|
| 32 |
import torchaudio
|
| 33 |
import numpy as np
|
| 34 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
# Neo4j imports
|
|
@@ -84,7 +85,7 @@ logging.basicConfig(level=logging.DEBUG)
|
|
| 84 |
|
| 85 |
|
| 86 |
# embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
|
| 87 |
-
embeddings
|
| 88 |
|
| 89 |
|
| 90 |
#Initialization
|
|
@@ -121,12 +122,12 @@ gpt4o_mini_model = initialize_gpt4o_mini_model()
|
|
| 121 |
|
| 122 |
# Existing embeddings and vector store for GPT-4o
|
| 123 |
gpt_embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
|
| 124 |
-
gpt_vectorstore = PineconeVectorStore(index_name="italyv109102024", embedding=
|
| 125 |
gpt_retriever = gpt_vectorstore.as_retriever(search_kwargs={'k': 5})
|
| 126 |
|
| 127 |
# New vector store setup for Phi-3.5
|
| 128 |
-
phi_embeddings =
|
| 129 |
-
phi_vectorstore = PineconeVectorStore(index_name="italyv109102024", embedding=
|
| 130 |
phi_retriever = phi_vectorstore.as_retriever(search_kwargs={'k': 5})
|
| 131 |
|
| 132 |
|
|
@@ -340,13 +341,12 @@ Ti prego di fornire i dettagli riguardanti il documento che sto per condividere,
|
|
| 340 |
Ecco i dettagli del documento da considerare:
|
| 341 |
- Nome del documento:
|
| 342 |
- Pagina:
|
| 343 |
-
- Altre informazioni necessarie
|
| 344 |
-
<|end|>
|
| 345 |
<|user|>
|
| 346 |
{context}
|
| 347 |
Question: {question}<|end|>
|
| 348 |
<|assistant|>
|
| 349 |
-
Sure! Here's the information
|
| 350 |
"""
|
| 351 |
|
| 352 |
|
|
@@ -463,129 +463,153 @@ Detailed Answer:
|
|
| 463 |
|
| 464 |
import traceback
|
| 465 |
|
| 466 |
-
def generate_answer(message, choice, retrieval_mode, selected_model):
|
| 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 |
|
|
@@ -1071,16 +1095,32 @@ def handle_retrieval_mode_change(choice):
|
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1074 |
def handle_model_choice_change(selected_model):
|
| 1075 |
-
if selected_model == "LM-2":
|
| 1076 |
# Disable retrieval mode and select style when LM-2 is selected
|
| 1077 |
-
return
|
| 1078 |
-
|
| 1079 |
-
|
| 1080 |
-
|
|
|
|
| 1081 |
else:
|
| 1082 |
-
#
|
| 1083 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1084 |
|
| 1085 |
#Flux Coding
|
| 1086 |
|
|
@@ -1340,9 +1380,9 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
|
| 1340 |
state = gr.State()
|
| 1341 |
|
| 1342 |
chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
|
| 1343 |
-
choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational")
|
| 1344 |
-
retrieval_mode = gr.Radio(label="Retrieval Mode", choices=["VDB", "KGF"], value="VDB")
|
| 1345 |
-
model_choice = gr.Dropdown(label="Choose Model", choices=["LM-
|
| 1346 |
|
| 1347 |
# Link the dropdown change to handle_model_choice_change
|
| 1348 |
model_choice.change(fn=handle_model_choice_change, inputs=model_choice, outputs=[retrieval_mode, choice, choice])
|
|
|
|
| 32 |
import torchaudio
|
| 33 |
import numpy as np
|
| 34 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 35 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 36 |
|
| 37 |
|
| 38 |
# Neo4j imports
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
# embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
|
| 88 |
+
embeddings= HuggingFaceEmbeddings(model="sentence-transformers/all-mpnet-base-v2")
|
| 89 |
|
| 90 |
|
| 91 |
#Initialization
|
|
|
|
| 122 |
|
| 123 |
# Existing embeddings and vector store for GPT-4o
|
| 124 |
gpt_embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
|
| 125 |
+
gpt_vectorstore = PineconeVectorStore(index_name="italyv109102024", embedding=embeddings)
|
| 126 |
gpt_retriever = gpt_vectorstore.as_retriever(search_kwargs={'k': 5})
|
| 127 |
|
| 128 |
# New vector store setup for Phi-3.5
|
| 129 |
+
phi_embeddings = embeddings_phi
|
| 130 |
+
phi_vectorstore = PineconeVectorStore(index_name="italyv109102024", embedding=embeddings)
|
| 131 |
phi_retriever = phi_vectorstore.as_retriever(search_kwargs={'k': 5})
|
| 132 |
|
| 133 |
|
|
|
|
| 341 |
Ecco i dettagli del documento da considerare:
|
| 342 |
- Nome del documento:
|
| 343 |
- Pagina:
|
| 344 |
+
- Altre informazioni necessarie:.<|end|>
|
|
|
|
| 345 |
<|user|>
|
| 346 |
{context}
|
| 347 |
Question: {question}<|end|>
|
| 348 |
<|assistant|>
|
| 349 |
+
Sure! Here's the information:
|
| 350 |
"""
|
| 351 |
|
| 352 |
|
|
|
|
| 463 |
|
| 464 |
import traceback
|
| 465 |
|
| 466 |
+
# def generate_answer(message, choice, retrieval_mode, selected_model):
|
| 467 |
+
# logging.debug(f"generate_answer called with choice: {choice}, retrieval_mode: {retrieval_mode}, and selected_model: {selected_model}")
|
| 468 |
+
|
| 469 |
+
# # Logic for disabling options for Phi-3.5
|
| 470 |
+
# if selected_model == "LM-2":
|
| 471 |
+
# choice = None
|
| 472 |
+
# retrieval_mode = None
|
| 473 |
+
|
| 474 |
+
# try:
|
| 475 |
+
# # Select the appropriate template based on the choice and model
|
| 476 |
+
# if choice == "Details" and selected_model == chat_model1: # GPT-4o-mini
|
| 477 |
+
# prompt_template = PromptTemplate(input_variables=["context", "question"], template=gpt4o_mini_template_details)
|
| 478 |
+
# elif choice == "Details":
|
| 479 |
+
# prompt_template = QA_CHAIN_PROMPT_1
|
| 480 |
+
# elif choice == "Conversational":
|
| 481 |
+
# prompt_template = QA_CHAIN_PROMPT_2
|
| 482 |
+
# else:
|
| 483 |
+
# prompt_template = QA_CHAIN_PROMPT_1 # Fallback to template1
|
| 484 |
+
|
| 485 |
+
# # # Handle hotel-related queries
|
| 486 |
+
# # if "hotel" in message.lower() or "hotels" in message.lower() and "birmingham" in message.lower():
|
| 487 |
+
# # logging.debug("Handling hotel-related query")
|
| 488 |
+
# # response = fetch_google_hotels()
|
| 489 |
+
# # logging.debug(f"Hotel response: {response}")
|
| 490 |
+
# # return response, extract_addresses(response)
|
| 491 |
+
|
| 492 |
+
# # # Handle restaurant-related queries
|
| 493 |
+
# # if "restaurant" in message.lower() or "restaurants" in message.lower() and "birmingham" in message.lower():
|
| 494 |
+
# # logging.debug("Handling restaurant-related query")
|
| 495 |
+
# # response = fetch_yelp_restaurants()
|
| 496 |
+
# # logging.debug(f"Restaurant response: {response}")
|
| 497 |
+
# # return response, extract_addresses(response)
|
| 498 |
+
|
| 499 |
+
# # # Handle flight-related queries
|
| 500 |
+
# # if "flight" in message.lower() or "flights" in message.lower() and "birmingham" in message.lower():
|
| 501 |
+
# # logging.debug("Handling flight-related query")
|
| 502 |
+
# # response = fetch_google_flights()
|
| 503 |
+
# # logging.debug(f"Flight response: {response}")
|
| 504 |
+
# # return response, extract_addresses(response)
|
| 505 |
+
|
| 506 |
+
# # Retrieval-based response
|
| 507 |
+
# if retrieval_mode == "VDB":
|
| 508 |
+
# logging.debug("Using VDB retrieval mode")
|
| 509 |
+
# if selected_model == chat_model:
|
| 510 |
+
# logging.debug("Selected model: LM-1")
|
| 511 |
+
# retriever = gpt_retriever
|
| 512 |
+
# context = retriever.get_relevant_documents(message)
|
| 513 |
+
# logging.debug(f"Retrieved context: {context}")
|
| 514 |
+
|
| 515 |
+
# prompt = prompt_template.format(context=context, question=message)
|
| 516 |
+
# logging.debug(f"Generated prompt: {prompt}")
|
| 517 |
+
|
| 518 |
+
# qa_chain = RetrievalQA.from_chain_type(
|
| 519 |
+
# llm=chat_model,
|
| 520 |
+
# chain_type="stuff",
|
| 521 |
+
# retriever=retriever,
|
| 522 |
+
# chain_type_kwargs={"prompt": prompt_template}
|
| 523 |
+
# )
|
| 524 |
+
# response = qa_chain({"query": message})
|
| 525 |
+
# logging.debug(f"LM-1 response: {response}")
|
| 526 |
+
# return response['result'], extract_addresses(response['result'])
|
| 527 |
|
| 528 |
+
# elif selected_model == chat_model1:
|
| 529 |
+
# logging.debug("Selected model: LM-3")
|
| 530 |
+
# retriever = gpt_retriever
|
| 531 |
+
# context = retriever.get_relevant_documents(message)
|
| 532 |
+
# logging.debug(f"Retrieved context: {context}")
|
| 533 |
+
|
| 534 |
+
# prompt = prompt_template.format(context=context, question=message)
|
| 535 |
+
# logging.debug(f"Generated prompt: {prompt}")
|
| 536 |
+
|
| 537 |
+
# qa_chain = RetrievalQA.from_chain_type(
|
| 538 |
+
# llm=chat_model1,
|
| 539 |
+
# chain_type="stuff",
|
| 540 |
+
# retriever=retriever,
|
| 541 |
+
# chain_type_kwargs={"prompt": prompt_template}
|
| 542 |
+
# )
|
| 543 |
+
# response = qa_chain({"query": message})
|
| 544 |
+
# logging.debug(f"LM-3 response: {response}")
|
| 545 |
+
# return response['result'], extract_addresses(response['result'])
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
+
# elif selected_model == phi_pipe:
|
| 550 |
+
# logging.debug("Selected model: LM-2")
|
| 551 |
+
# retriever = phi_retriever
|
| 552 |
+
# context_documents = retriever.get_relevant_documents(message)
|
| 553 |
+
# context = "\n".join([doc.page_content for doc in context_documents])
|
| 554 |
+
# logging.debug(f"Retrieved context for LM-2: {context}")
|
| 555 |
+
|
| 556 |
+
# # Use the correct template variable
|
| 557 |
+
# prompt = phi_custom_template.format(context=context, question=message)
|
| 558 |
+
# logging.debug(f"Generated LM-2 prompt: {prompt}")
|
| 559 |
+
|
| 560 |
+
# response = selected_model(prompt, **{
|
| 561 |
+
# "max_new_tokens": 400,
|
| 562 |
+
# "return_full_text": True,
|
| 563 |
+
# "temperature": 0.7,
|
| 564 |
+
# "do_sample": True,
|
| 565 |
+
# })
|
| 566 |
+
|
| 567 |
+
# if response:
|
| 568 |
+
# generated_text = response[0]['generated_text']
|
| 569 |
+
# logging.debug(f"LM-2 Response: {generated_text}")
|
| 570 |
+
# cleaned_response = clean_response(generated_text)
|
| 571 |
+
# return cleaned_response, extract_addresses(cleaned_response)
|
| 572 |
+
# else:
|
| 573 |
+
# logging.error("LM-2 did not return any response.")
|
| 574 |
+
# return "No response generated.", []
|
| 575 |
+
|
| 576 |
+
# elif retrieval_mode == "KGF":
|
| 577 |
+
# logging.debug("Using KGF retrieval mode")
|
| 578 |
+
# response = chain_neo4j.invoke({"question": message})
|
| 579 |
+
# logging.debug(f"KGF response: {response}")
|
| 580 |
+
# return response, extract_addresses(response)
|
| 581 |
+
# else:
|
| 582 |
+
# logging.error("Invalid retrieval mode selected.")
|
| 583 |
+
# return "Invalid retrieval mode selected.", []
|
| 584 |
+
|
| 585 |
+
# except Exception as e:
|
| 586 |
+
# logging.error(f"Error in generate_answer: {str(e)}")
|
| 587 |
+
# logging.error(traceback.format_exc())
|
| 588 |
+
# return "Sorry, I encountered an error while processing your request.", []
|
| 589 |
|
| 590 |
+
def generate_answer(message, choice, retrieval_mode, selected_model):
|
| 591 |
+
# Logic for Phi-3.5
|
| 592 |
+
if selected_model == phi_pipe: # LM-2 Phi-3.5 selected
|
| 593 |
+
retriever = phi_retriever
|
| 594 |
+
context_documents = retriever.get_relevant_documents(message)
|
| 595 |
+
context = "\n".join([doc.page_content for doc in context_documents])
|
| 596 |
+
|
| 597 |
+
# Use the correct template for Phi-3.5
|
| 598 |
+
prompt = phi_custom_template.format(context=context, question=message)
|
| 599 |
+
|
| 600 |
+
response = selected_model(prompt, **{
|
| 601 |
+
"max_new_tokens": 400,
|
| 602 |
+
"return_full_text": True,
|
| 603 |
+
"temperature": 0.7,
|
| 604 |
+
"do_sample": True,
|
| 605 |
+
})
|
| 606 |
+
|
| 607 |
+
if response:
|
| 608 |
+
generated_text = response[0]['generated_text']
|
| 609 |
+
cleaned_response = clean_response(generated_text)
|
| 610 |
+
return cleaned_response, extract_addresses(cleaned_response)
|
| 611 |
+
else:
|
| 612 |
+
return "No response generated.", []
|
| 613 |
|
| 614 |
|
| 615 |
|
|
|
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
+
# def handle_model_choice_change(selected_model):
|
| 1099 |
+
# if selected_model == "LM-2":
|
| 1100 |
+
# # Disable retrieval mode and select style when LM-2 is selected
|
| 1101 |
+
# return gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
|
| 1102 |
+
# elif selected_model == "LM-1":
|
| 1103 |
+
# # Enable retrieval mode and select style for LM-1
|
| 1104 |
+
# return gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
|
| 1105 |
+
# else:
|
| 1106 |
+
# # Default case: allow interaction
|
| 1107 |
+
# return gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
|
| 1108 |
+
|
| 1109 |
def handle_model_choice_change(selected_model):
|
| 1110 |
+
if selected_model == "LM-2": # When LM-2 (Phi-3.5) is selected
|
| 1111 |
# Disable retrieval mode and select style when LM-2 is selected
|
| 1112 |
+
return (
|
| 1113 |
+
gr.update(interactive=False), # Disable retrieval mode
|
| 1114 |
+
gr.update(interactive=False), # Disable style (Details/Conversational)
|
| 1115 |
+
gr.update(interactive=False) # Disable the model choice itself
|
| 1116 |
+
)
|
| 1117 |
else:
|
| 1118 |
+
# Disable GPT-4o, GPT-4o-mini, and KGF, only Phi-3.5 works
|
| 1119 |
+
return (
|
| 1120 |
+
gr.update(interactive=True), # Allow retrieval mode for other models
|
| 1121 |
+
gr.update(interactive=True), # Allow style options for other models
|
| 1122 |
+
gr.update(interactive=True) # Allow other models to be selected
|
| 1123 |
+
)
|
| 1124 |
|
| 1125 |
#Flux Coding
|
| 1126 |
|
|
|
|
| 1380 |
state = gr.State()
|
| 1381 |
|
| 1382 |
chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
|
| 1383 |
+
choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational",interactive=False,visible=False)
|
| 1384 |
+
retrieval_mode = gr.Radio(label="Retrieval Mode", choices=["VDB", "KGF"], value="VDB",interactive=False,visible=False)
|
| 1385 |
+
model_choice = gr.Dropdown(label="Choose Model", choices=["LM-2"], value="LM-2")
|
| 1386 |
|
| 1387 |
# Link the dropdown change to handle_model_choice_change
|
| 1388 |
model_choice.change(fn=handle_model_choice_change, inputs=model_choice, outputs=[retrieval_mode, choice, choice])
|