Spaces:
Build error
Build error
Update app.py
Browse files1234567891011121314123456789101121221213141516172627282930313233343536373839404142434445464748495051181920212223242526272829303132333435363738394041424344454647484934 unmodified lines848586929387888990919596979899929394959697989910010110219 unmodified lines122123124125125import 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 partitionimport nltkfrom docx import Document # Added missing import
# Download the punkt tokenizer datanltk.download("punkt")
# Replace with your Gemini API keygemini_api_key = 'AIzaSyCJBozoq3J5jTemmn_XF6EnWpoRQkA8NCg'# 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 PDF with open(file_path, "rb") as file: reader = PyPDF2.PdfReader(file) text = "" for page in reader.pages: text += page.extract_text() return text
elif file_path.endswith(".docx"): # Extract text from DOCX doc = Document(file_path) text = "\n".join([para.text for para in doc.paragraphs]) return text
elif file_path.endswith(".txt"): # Extract text from TXT with open(file_path, "r") as file: text = file.read() return text
else: # Use unstructured for other file types elements = partition(file_path) text = "\n".join([str(el) for el in elements]) return text if file_path.endswith(".pdf"): # Extract text from PDF with open(file_path, "rb") as file: reader = PyPDF2.PdfReader(file) text = "" for page in reader.pages: text += page.extract_text() return text
elif file_path.endswith(".docx"): # Extract text from DOCX doc = Document(file_path) text = "\n".join([para.text for para in doc.paragraphs]) return text
elif file_path.endswith(".txt"): # Extract text from TXT with open(file_path, "r") as file: text = file.read() return text
else: # Use unstructured for other file types elements = partition(file_path) text = "\n".join([str(el) for el in elements]) return text except 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 ] }
response = requests.post(url, json=data, headers=headers) response_data = response.json() try: response = requests.post(url, json=data, headers=headers) response.raise_for_status() # Raise an error for bad status codes 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_message else: return "Sorry, I couldn't generate a response." if "candidates" in response_data and len(response_data["candidates"]) > 0: bot_message = response_data["candidates"][0]["content"]["parts"][0]["text"].strip() return bot_message else: 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 lines submit_button.click(chatbot_response, inputs=[msg, state], outputs=[chatbot, msg])
# Launch the Gradio appdemo.launch(debug=True)demo.launch(debug=True)app.py+50-50