Update app.py
Browse files18 unmodified lines192021222223242514 unmodified lines40414243444546306 unmodified lines353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405102 unmodified lines50850951046351151251351451551651751851952052152246852352452552618 unmodified linesimport shutilimport pandas as pdfrom docx import Document as DocxDocument
import google.generativeai as genai
# Set up basic configuration for logginglogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')14 unmodified lines "@cf/meta/llama-3.1-8b-instruct", "mistralai/Mistral-Nemo-Instruct-2407", "mistralai/Mathstral-7B-v0.1", "gemini-1.5-flash", "duckduckgo/gpt-4o-mini", "duckduckgo/claude-3-haiku", "duckduckgo/llama-3.1-70b",306 unmodified lines except Exception as e: return f"An error occurred during summarization: {str(e)}"
def get_response_from_gemini(query, context, file_type, num_calls=1, temperature=0.2): # Configure the Gemini API genai.configure(api_key=os.environ["GEMINI_API_KEY"])
# Define the model model = genai.GenerativeModel( model_name="gemini-1.5-flash", generation_config={ "temperature": temperature, "top_p": 1, "top_k": 1, "max_output_tokens": 2048, }, )
# Create the system instruction based on file type if file_type == "excel": system_instruction = """You are a highly specialized data analyst with expertise in Excel spreadsheets. Your task is to analyze the provided Excel data and answer the user's query accurately and concisely. Focus on identifying key metrics, trends, and significant details relevant to the query. Do not make assumptions or include information not explicitly supported by the dataset.""" elif file_type == "pdf": system_instruction = """You are a highly specialized document analyst with expertise in extracting information from PDF documents. Your task is to analyze the provided PDF content and answer the user's query accurately and comprehensively. Focus on key points, important details, and relevant information from the document. Ensure your response is strictly based on the provided context.""" else: raise ValueError("Invalid file type. Use 'excel' or 'pdf'.")
# Prepare the chat session chat_session = model.start_chat(history=[])
full_response = "" for _ in range(num_calls): try: # Send the message with context and query response = chat_session.send_message( f"{system_instruction}\n\nContext:\n{context}\n\nUser query: {query}" ) full_response += response.text + "\n" except Exception as e: print(f"Error in generating response from Gemini: {str(e)}") return f"An error occurred with the Gemini model: {str(e)}. Please try again."
return full_response.strip()
def get_response_from_excel(query, model, context, num_calls=3, temperature=0.2): logging.info(f"Getting response from Excel using model: {model}") 102 unmodified lines
# Process Excel documents if excel_docs: for response in get_response_from_excel(message, model, excel_context, num_calls, temperature): if model == "gemini-1.5-flash": response = get_response_from_gemini(message, excel_context, "excel", num_calls, temperature) yield response else: for response in get_response_from_excel(message, model, excel_context, num_calls, temperature): yield response
# Process other documents (PDF, Word) if other_docs: if model == "gemini-1.5-flash": response = get_response_from_gemini(message, other_context, "pdf", num_calls, temperature) yield response if model == "@cf/meta/llama-3.1-8b-instruct": elif model == "@cf/meta/llama-3.1-8b-instruct": for response in get_response_from_cloudflare(prompt="", context=other_context, query=message, num_calls=num_calls, temperature=temperature, search_type="document"): yield response else:app.py+58-3