Spaces:
Sleeping
Sleeping
Update app.py
Browse files1232 unmodified lines678910111213141516171845 unmodified lines646566676869701 unmodified line72737475767778798081828384858687888923 unmodified lines11311411511611711811912012112212312412512612712812913013113213313410 unmodified lines145146147148149150import gradio as grfrom pypdf import PdfReaderfrom sentence_transformers import SentenceTransformer2 unmodified linesfrom transformers import pipeline# -------------------------------# Load models (lightweight)# -------------------------------embed_model = SentenceTransformer('all-MiniLM-L6-v2')qa_pipeline = pipeline("text-generation",model="distilgpt2" # lightweight & safe for Spaces)# -------------------------------45 unmodified linesreturn " ".join(results)# -------------------------------# Generate answer# -------------------------------def answer_question(query, index, chunks):context = retrieve(query, index, chunks)1 unmodified lineif not context.strip():return "β οΈ No relevant content found."prompt = f"Answer based on context:\n{context}\n\nQuestion: {query}\nAnswer:"result = qa_pipeline(prompt,max_new_tokens=120,do_sample=True,temperature=0.7)answer = result[0]['generated_text']return answer# -------------------------------# Global variables23 unmodified linesreturn "β PDF processed! Ask questions now."# -------------------------------# Chat function# -------------------------------def respond(message, chat_history):if index is None:bot_reply = "β Upload and process PDF first."else:try:bot_reply = answer_question(message, index, chunks)except Exception as e:bot_reply = f"β Error: {str(e)}"chat_history.append((message, bot_reply))return "", chat_history# -------------------------------# UI (ChatGPT Style)# -------------------------------with gr.Blocks() as app:gr.Markdown("# π€ Course Material Chatbot")10 unmodified linesupload_btn.click(process_pdf, inputs=file_input, outputs=status)msg.submit(respond, [msg, chatbot], [msg, chatbot])clear.click(lambda: None, None, chatbot, queue=False)app.launch()123452 unmodified lines8910111213141516171819202145 unmodified lines676869707172731 unmodified line757677787980818283848586878823 unmodified lines11211311411511611711811912012112212312412512612712812913010 unmodified lines141142143144145146import gradio as grfrom pypdf import PdfReaderfrom sentence_transformers import SentenceTransformer2 unmodified linesfrom transformers import pipeline# -------------------------------# Load models# -------------------------------embed_model = SentenceTransformer('all-MiniLM-L6-v2')# β Proper QA model (IMPORTANT FIX)qa_pipeline = pipeline("question-answering",model="distilbert-base-cased-distilled-squad")# -------------------------------45 unmodified linesreturn " ".join(results)# -------------------------------# Generate answer (FIXED)# -------------------------------def answer_question(query, index, chunks):context = retrieve(query, index, chunks)1 unmodified lineif not context.strip():return "β οΈ No relevant content found."try:result = qa_pipeline(question=query,context=context)return result['answer']except Exception as e:return f"β Error: {str(e)}"# -------------------------------# Global variables23 unmodified linesreturn "β PDF processed! Ask questions now."# -------------------------------# Chat function (ChatGPT style)# -------------------------------def respond(message, chat_history):if index is None:bot_reply = "β Upload and process PDF first."else:bot_reply = answer_question(message, index, chunks)chat_history.append((message, bot_reply))return "", chat_history# -------------------------------# UI# -------------------------------with gr.Blocks() as app:gr.Markdown("# π€ Course Material Chatbot")10 unmodified linesupload_btn.click(process_pdf, inputs=file_input, outputs=status)msg.submit(respond, [msg, chatbot], [msg, chatbot])clear.click(lambda: [], None, chatbot, queue=False)app.launch()
app.py+19-23