Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -472,11 +472,11 @@ def get_response_from_excel(query, model, context, num_calls=3, temperature=0.2)
|
|
472 |
logging.info("Finished generating response for Excel data")
|
473 |
|
474 |
def get_response_from_llama(query, model, selected_docs, file_type, num_calls=1, temperature=0.2):
|
475 |
-
logging.info(f"
|
476 |
|
|
|
477 |
client = InferenceClient(model, token=huggingface_token)
|
478 |
-
|
479 |
-
|
480 |
if file_type == "excel":
|
481 |
# Excel functionality
|
482 |
system_instruction = """You are a highly specialized Python programmer with deep expertise in data analysis and visualization using Excel spreadsheets.
|
@@ -492,11 +492,8 @@ def get_response_from_llama(query, model, selected_docs, file_type, num_calls=1,
|
|
492 |
relevant_docs = retriever.get_relevant_documents(query)
|
493 |
context = "\n".join([doc.page_content for doc in relevant_docs if doc.metadata["source"] in selected_docs])
|
494 |
|
495 |
-
|
496 |
-
|
497 |
-
{"role": "user", "content": f"Based on the following data extracted from Excel spreadsheets:\n{context}\n\nPlease provide the Python code needed to execute the following task: '{query}'. Ensure that the code is derived directly from the dataset. If a chart is requested, use the matplotlib library to generate the appropriate visualization."}
|
498 |
-
]
|
499 |
-
|
500 |
elif file_type == "pdf":
|
501 |
# PDF functionality
|
502 |
embed = get_embeddings()
|
@@ -511,41 +508,32 @@ def get_response_from_llama(query, model, selected_docs, file_type, num_calls=1,
|
|
511 |
Your goal is to provide accurate, detailed, and precise summaries based on the context provided.
|
512 |
Avoid making assumptions or adding information that is not explicitly supported by the context from the PDF documents."""
|
513 |
|
514 |
-
|
515 |
-
{"role": "system", "content": system_instruction},
|
516 |
-
{"role": "user", "content": f"Using the following context from the PDF documents:\n{context_str}\n\nPlease generate a step-by-step reasoning before arriving at a comprehensive and accurate summary addressing the following question: '{query}'. Ensure your response is strictly based on the provided context, highlighting key metrics, trends, and significant details relevant to the query. Avoid any speculative or unverified information."}
|
517 |
-
]
|
518 |
|
519 |
else:
|
520 |
raise ValueError("Invalid file type. Use 'excel' or 'pdf'.")
|
521 |
-
|
522 |
-
# logging.info(f"Prepared messages: {messages}")
|
523 |
-
|
524 |
full_response = ""
|
525 |
-
for
|
526 |
-
logging.info(f"Starting API call {i+1}/{num_calls}")
|
527 |
try:
|
528 |
-
|
529 |
-
|
530 |
-
|
|
|
531 |
temperature=temperature,
|
532 |
-
stream=True,
|
533 |
):
|
534 |
-
|
535 |
-
|
536 |
-
chunk = message.choices[0].delta.content
|
537 |
full_response += chunk
|
538 |
-
|
539 |
-
yield full_response
|
540 |
except Exception as e:
|
541 |
-
logging.error(f"Error during API call
|
542 |
yield f"An error occurred with the Llama model: {str(e)}. Please try again."
|
543 |
|
544 |
if not full_response:
|
545 |
logging.warning("No response generated from the Llama model")
|
546 |
yield "No response generated from the Llama model."
|
547 |
-
else:
|
548 |
-
logging.info(f"Final response length: {len(full_response)}")
|
549 |
|
550 |
# Modify the existing respond function to handle both PDF and web search
|
551 |
def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
|
|
|
472 |
logging.info("Finished generating response for Excel data")
|
473 |
|
474 |
def get_response_from_llama(query, model, selected_docs, file_type, num_calls=1, temperature=0.2):
|
475 |
+
logging.info(f"Getting response from Llama using model: {model}")
|
476 |
|
477 |
+
# Initialize the Hugging Face client
|
478 |
client = InferenceClient(model, token=huggingface_token)
|
479 |
+
|
|
|
480 |
if file_type == "excel":
|
481 |
# Excel functionality
|
482 |
system_instruction = """You are a highly specialized Python programmer with deep expertise in data analysis and visualization using Excel spreadsheets.
|
|
|
492 |
relevant_docs = retriever.get_relevant_documents(query)
|
493 |
context = "\n".join([doc.page_content for doc in relevant_docs if doc.metadata["source"] in selected_docs])
|
494 |
|
495 |
+
prompt = f"{system_instruction}\n\nBased on the following data extracted from Excel spreadsheets:\n{context}\n\nPlease provide the Python code needed to execute the following task: '{query}'. Ensure that the code is derived directly from the dataset. If a chart is requested, use the matplotlib library to generate the appropriate visualization."
|
496 |
+
|
|
|
|
|
|
|
497 |
elif file_type == "pdf":
|
498 |
# PDF functionality
|
499 |
embed = get_embeddings()
|
|
|
508 |
Your goal is to provide accurate, detailed, and precise summaries based on the context provided.
|
509 |
Avoid making assumptions or adding information that is not explicitly supported by the context from the PDF documents."""
|
510 |
|
511 |
+
prompt = f"{system_instruction}\n\nUsing the following context from the PDF documents:\n{context_str}\n\nPlease generate a step-by-step reasoning before arriving at a comprehensive and accurate summary addressing the following question: '{query}'. Ensure your response is strictly based on the provided context, highlighting key metrics, trends, and significant details relevant to the query. Avoid any speculative or unverified information."
|
|
|
|
|
|
|
512 |
|
513 |
else:
|
514 |
raise ValueError("Invalid file type. Use 'excel' or 'pdf'.")
|
515 |
+
|
|
|
|
|
516 |
full_response = ""
|
517 |
+
for _ in range(num_calls):
|
|
|
518 |
try:
|
519 |
+
# Generate content with streaming enabled
|
520 |
+
for response in client.text_generation(
|
521 |
+
prompt=prompt,
|
522 |
+
max_new_tokens=2000,
|
523 |
temperature=temperature,
|
524 |
+
stream=True,
|
525 |
):
|
526 |
+
if response.token.text:
|
527 |
+
chunk = response.token.text
|
|
|
528 |
full_response += chunk
|
529 |
+
yield full_response # Yield the accumulated response so far
|
|
|
530 |
except Exception as e:
|
531 |
+
logging.error(f"Error during API call: {str(e)}")
|
532 |
yield f"An error occurred with the Llama model: {str(e)}. Please try again."
|
533 |
|
534 |
if not full_response:
|
535 |
logging.warning("No response generated from the Llama model")
|
536 |
yield "No response generated from the Llama model."
|
|
|
|
|
537 |
|
538 |
# Modify the existing respond function to handle both PDF and web search
|
539 |
def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
|