Spaces:
Build error
Build error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535434 unmodified lines899091929394959697989910010110219 unmodified lines122123124125import nltk# Ensure necessary NLTK resources are downloadedtry:nltk.data.find('tokenizers/punkt')except LookupError:nltk.download('punkt')try:nltk.data.find('tokenizers/punkt_tab/english')except LookupError:nltk.download('punkt_tab') # This will download the missing 'punkt_tab' resource# Your existing imports and functions follow...import gradio as grimport requestsimport osimport PyPDF2from unstructured.partition.auto import partition# Replace with your Gemini API keygemini_api_key = 'AIzaSyCJBozoq3J5jTemmn_XF6EnWpoRQkA8NCg'# Function to load and extract text from a filedef load_and_extract_text(file_path):if file_path.endswith(".pdf"):# Extract text from PDFwith open(file_path, "rb") as file:reader = PyPDF2.PdfReader(file)text = ""for page in reader.pages:text += page.extract_text()return textelif file_path.endswith(".docx"):# Extract text from DOCXdoc = Document(file_path)text = "\n".join([para.text for para in doc.paragraphs])return textelif file_path.endswith(".txt"):# Extract text from TXTwith open(file_path, "r") as file:text = file.read()return textelse:# Use unstructured for other file typeselements = partition(file_path)text = "\n".join([str(el) for el in elements])return text# Function to load all resources from a folderdef load_resources(folder_path):34 unmodified lines]}response = requests.post(url, json=data, headers=headers)response_data = response.json()if "candidates" in response_data and len(response_data["candidates"]) > 0:bot_message = response_data["candidates"][0]["content"]["parts"][0]["text"].strip()return bot_messageelse:return "Sorry, I couldn't generate a response."# Function to handle the chat interaction and pass the messages back and forthdef chatbot_response(message, history):19 unmodified linessubmit_button.click(chatbot_response, inputs=[msg, state], outputs=[chatbot, msg])# Launch the Gradio appdemo.launch(debug=True)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484934 unmodified lines8485868788899091929394959697989910010110219 unmodified lines122123124125import gradio as grimport requestsimport osimport PyPDF2from unstructured.partition.auto import partitionimport nltkfrom docx import Document # Added missing import# Download the punkt tokenizer datanltk.download("punkt")# Replace with your Gemini API key (use environment variables in production)gemini_api_key = os.getenv("GEMINI_API_KEY", 'AIzaSyCJBozoq3J5jTemmn_XF6EnWpoRQkA8NCg')# Function to load and extract text from a filedef load_and_extract_text(file_path):try:if file_path.endswith(".pdf"):# Extract text from PDFwith open(file_path, "rb") as file:reader = PyPDF2.PdfReader(file)text = ""for page in reader.pages:text += page.extract_text()return textelif file_path.endswith(".docx"):# Extract text from DOCXdoc = Document(file_path)text = "\n".join([para.text for para in doc.paragraphs])return textelif file_path.endswith(".txt"):# Extract text from TXTwith open(file_path, "r") as file:text = file.read()return textelse:# Use unstructured for other file typeselements = partition(file_path)text = "\n".join([str(el) for el in elements])return textexcept Exception as e:print(f"Error processing file {file_path}: {e}")return ""# Function to load all resources from a folderdef load_resources(folder_path):34 unmodified lines]}try:response = requests.post(url, json=data, headers=headers)response.raise_for_status() # Raise an error for bad status codesresponse_data = response.json()if "candidates" in response_data and len(response_data["candidates"]) > 0:bot_message = response_data["candidates"][0]["content"]["parts"][0]["text"].strip()return bot_messageelse:return "Sorry, I couldn't generate a response."except requests.exceptions.RequestException as e:print(f"API request failed: {e}")return "Sorry, I couldn't generate a response due to an API error."# Function to handle the chat interaction and pass the messages back and forthdef chatbot_response(message, history):19 unmodified linessubmit_button.click(chatbot_response, inputs=[msg, state], outputs=[chatbot, msg])# Launch the Gradio appdemo.launch(debug=True)
app.py+50-50