eikarna commited on
Commit ·
d9760ae
1
Parent(s): 853734d
Fix: File Upload Session
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264import streamlit as stimport requestsimport loggingimport timefrom typing import Dict, Any, Optional, Listimport osfrom PIL import Imageimport pytesseractimport fitz # PyMuPDFfrom io import BytesIOimport hashlibfrom sentence_transformers import SentenceTransformerimport numpy as npfrom pathlib import Pathimport pickleimport tempfile# Configure logginglogging.basicConfig(level=logging.INFO,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')logger = logging.getLogger(__name__)# Initialize SBERT model for embeddings@st.cache_resourcedef load_embedding_model():return SentenceTransformer('all-MiniLM-L6-v2')# Modified Vector Store Classclass SimpleVectorStore:def __init__(self):self.documents = []self.embeddings = []def add_document(self, text: str, embedding: np.ndarray):self.documents.append(text)self.embeddings.append(embedding)def search(self, query_embedding: np.ndarray, top_k: int = 3) -> List[str]:if not self.embeddings:return []similarities = np.dot(self.embeddings, query_embedding)top_indices = np.argsort(similarities)[-top_k:][::-1]return [self.documents[i] for i in top_indices]# Document processing functionsdef process_text(text: str) -> List[str]:"""Split text into chunks."""# Simple splitting by sentences (can be improved with better chunking)chunks = text.split('. ')return [chunk + '.' for chunk in chunks if chunk]def process_image(image) -> str:"""Extract text from image using OCR."""try:text = pytesseract.image_to_string(image)return textexcept Exception as e:logger.error(f"Error processing image: {str(e)}")return ""def process_pdf(pdf_file) -> str:"""Extract text from PDF."""try:with tempfile.NamedTemporaryFile(delete=False) as tmp_file:tmp_file.write(pdf_file.read())tmp_file.flush()doc = fitz.open(tmp_file.name)text = ""for page in doc:text += page.get_text()doc.close()os.unlink(tmp_file.name)return textexcept Exception as e:logger.error(f"Error processing PDF: {str(e)}")return ""# Initialize session stateif "messages" not in st.session_state:st.session_state.messages = []if "request_timestamps" not in st.session_state:st.session_state.request_timestamps = []if "vector_store" not in st.session_state:st.session_state.vector_store = SimpleVectorStore()# Rate limiting configurationRATE_LIMIT_PERIOD = 60MAX_REQUESTS_PER_PERIOD = 30def check_rate_limit() -> bool:"""Check if we're within rate limits."""current_time = time.time()st.session_state.request_timestamps = [ts for ts in st.session_state.request_timestampsif current_time - ts < RATE_LIMIT_PERIOD]if len(st.session_state.request_timestamps) >= MAX_REQUESTS_PER_PERIOD:return Falsest.session_state.request_timestamps.append(current_time)return Truedef query(payload: Dict[str, Any], api_url: str) -> Optional[Dict[str, Any]]:"""Query the Hugging Face API with error handling and rate limiting."""if not check_rate_limit():raise Exception(f"Rate limit exceeded. Please wait {RATE_LIMIT_PERIOD} seconds.")try:headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}response = requests.post(api_url, headers=headers, json=payload, timeout=30)if response.status_code == 429:raise Exception("Too many requests. Please try again later.")response.raise_for_status()print(response.request.url)print(response.request.headers)print(response.request.body)print(response)return response.json()except requests.exceptions.JSONDecodeError as e:logger.error(f"API request failed: {str(e)}")raise# Enhanced response validationdef process_response(response: Dict[str, Any]) -> str:if not isinstance(response, list) or not response:raise ValueError("Invalid response format")if 'generated_text' not in response[0]:raise ValueError("Unexpected response structure")text = response[0]['generated_text'].strip()# Page configurationst.set_page_config(page_title="RAG-Enabled DeepSeek Chatbot",page_icon="🤖",layout="wide")# Sidebar configurationwith st.sidebar:st.header("Model Configuration")st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)")model_options = ["deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",]selected_model = st.selectbox("Select Model", model_options, index=0)system_message = st.text_area("System Message",value="You are a friendly chatbot with RAG capabilities. Use the provided context to answer questions accurately. If the context doesn't contain relevant information, say so.",height=100)max_tokens = st.slider("Max Tokens", 10, 4000, 100)temperature = st.slider("Temperature", 0.1, 4.0, 0.3)top_p = st.slider("Top-p", 0.1, 1.0, 0.6)# File upload sectionst.header("Upload Knowledge Base")uploaded_files = st.file_uploader("Upload files (PDF, Images, Text)",type=['pdf', 'png', 'jpg', 'jpeg', 'txt'],accept_multiple_files=True)# Process uploaded filesif uploaded_files:embedding_model = load_embedding_model()for file in uploaded_files:try:if file.type == "application/pdf":text = process_pdf(file)elif file.type.startswith("image/"):image = Image.open(file)text = process_image(image)else: # text filestext = file.getvalue().decode()chunks = process_text(text)for chunk in chunks:embedding = embedding_model.encode(chunk)st.session_state.vector_store.add_document(chunk, embedding)st.sidebar.success(f"Successfully processed {file.name}")except Exception as e:st.sidebar.error(f"Error processing {file.name}: {str(e)}")# Main chat interfacest.title("🤖 RAG-Enabled DeepSeek Chatbot")st.caption("Upload documents in the sidebar to enhance the chatbot's knowledge")# Display chat historyfor message in st.session_state.messages:with st.chat_message(message["role"]):st.markdown(message["content"])# Handle user inputif prompt := st.chat_input("Type your message..."):# Display user messagest.session_state.messages.append({"role": "user", "content": prompt})with st.chat_message("user"):st.markdown(prompt)try:with st.spinner("Generating response..."):embedding_model = load_embedding_model()query_embedding = embedding_model.encode(prompt)relevant_contexts = st.session_state.vector_store.search(query_embedding)# Dynamic context handlingcontext_text = "\n".join(relevant_contexts) if relevant_contexts else ""system_msg = (f"{system_message} Use the provided context to answer accurately."if context_textelse system_message)# Format for DeepSeek modelfull_prompt = f"""<|beginofutterance|>System: {system_msg}{context_text if context_text else ''}<|endofutterance|><|beginofutterance|>User: {prompt}<|endofutterance|><|beginofutterance|>Assistant:"""payload = {"inputs": full_prompt,"parameters": {"max_new_tokens": max_tokens,"temperature": temperature,"top_p": top_p,"return_full_text": False}}api_url = f"https://api-inference.huggingface.co/models/{selected_model}"# Get and process responseoutput = query(payload, api_url)if output:response_text = process_response(output)# Display assistant responsewith st.chat_message("assistant"):st.markdown(response_text)# Update chat historyst.session_state.messages.append({"role": "assistant","content": response_text})except Exception as e:logger.error(f"Error: {str(e)}", exc_info=True)st.error(f"Error: {str(e)}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147import streamlit as stimport requestsimport loggingfrom typing import Optional, Dict, Any# Configure logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)# ConstantsDEFAULT_SYSTEM_PROMPT = """You are a friendly Assistant. Provide clear, accurate, and brief answers.Keep responses polite, engaging, and to the point. If unsure, politely suggest alternatives."""MODEL_OPTIONS = ["deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"]API_BASE_URL = "https://api-inference.huggingface.co/models/"# Page configurationst.set_page_config(page_title="DeepSeek-AI R1 (32B)",page_icon="🤖",layout="centered")def initialize_session_state():"""Initialize all session state variables"""if "messages" not in st.session_state:st.session_state.messages = []if "api_failures" not in st.session_state:st.session_state.api_failures = 0def configure_sidebar() -> Dict[str, Any]:"""Create sidebar components and return settings"""with st.sidebar:st.header("Model Configuration")st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)")return {"model": st.selectbox("Select Model", MODEL_OPTIONS, index=0),"system_message": st.text_area("System Message",value=DEFAULT_SYSTEM_PROMPT,height=100),"max_tokens": st.slider("Max Tokens", 10, 4000, 100),"temperature": st.slider("Temperature", 0.1, 4.0, 0.3),"top_p": st.slider("Top-p", 0.1, 1.0, 0.6)}def format_deepseek_prompt(system_message: str, user_input: str) -> str:"""Format the prompt according to DeepSeek's required structure"""return f"""<|beginofutterance|>System: {system_message}<|endofutterance|><|beginofutterance|>User: {user_input}<|endofutterance|><|beginofutterance|>Assistant:"""def query_hf_api(payload: Dict[str, Any], api_url: str) -> Optional[Dict[str, Any]]:"""Handle API requests with improved error handling"""headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}try:response = requests.post(api_url,headers=headers,json=payload,timeout=30)response.raise_for_status()return response.json()except requests.exceptions.HTTPError as e:logger.error(f"HTTP Error: {e.response.status_code} - {e.response.text}")st.error(f"API Error: {e.response.status_code} - {e.response.text[:200]}")except requests.exceptions.RequestException as e:logger.error(f"Request failed: {str(e)}")st.error("Connection error. Please check your internet connection.")return Nonedef handle_chat_interaction(settings: Dict[str, Any]):"""Manage chat input/output and API communication"""if prompt := st.chat_input("Type your message..."):# Add user message to historyst.session_state.messages.append({"role": "user", "content": prompt})with st.chat_message("user"):st.markdown(prompt)try:with st.spinner("Generating response..."):# Format prompt according to model requirementsfull_prompt = format_deepseek_prompt(system_message=settings["system_message"],user_input=prompt)payload = {"inputs": full_prompt,"parameters": {"max_new_tokens": settings["max_tokens"],"temperature": settings["temperature"],"top_p": settings["top_p"]}}api_url = f"{API_BASE_URL}{settings['model']}"output = query_hf_api(payload, api_url)if output and isinstance(output, list):if 'generated_text' in output[0]:response_text = output[0]['generated_text'].strip()# Remove any remaining special tokensresponse_text = response_text.replace("<|endofutterance|>", "").strip()# Display and store responsewith st.chat_message("assistant"):st.markdown(response_text)st.session_state.messages.append({"role": "assistant", "content": response_text})return# Handle failed responsesst.session_state.api_failures += 1if st.session_state.api_failures > 2:st.error("Persistent API failures. Please check your API token and model selection.")except Exception as e:logger.error(f"Unexpected error: {str(e)}", exc_info=True)st.error("An unexpected error occurred. Please try again.")def display_chat_history():"""Render chat message history"""for message in st.session_state.messages:with st.chat_message(message["role"]):st.markdown(message["content"])def main():"""Main application flow"""initialize_session_state()settings = configure_sidebar()st.title("🤖 DeepSeek Chatbot")st.caption("Powered by Hugging Face Inference API - Configure in sidebar")display_chat_history()handle_chat_interaction(settings)if __name__ == "__main__":main()
app.py+126-243