import os import gradio as gr from transformers import pipeline from langchain_text_splitters import RecursiveCharacterTextSplitter # ========================= # LOAD DOCUMENT # ========================= def load_text(path="ai-geoth.txt"): if not os.path.exists(path): raise FileNotFoundError("ai-geoth.txt not found") with open(path, "r", encoding="utf-8") as f: return f.read() text = load_text() # ========================= # SPLIT DOCUMENT # ========================= splitter = RecursiveCharacterTextSplitter( chunk_size=500, chunk_overlap=100 ) chunks = splitter.split_text(text) # ========================= # EXTRACTIVE QA (LIGHTWEIGHT) # ========================= qa = pipeline( "question-answering", model="distilbert-base-uncased-distilled-squad", device=-1 ) # ========================= # SMART LONG ANSWER # ========================= def answer_question(question): answers = [] for chunk in chunks: try: result = qa(question=question, context=chunk) if result["score"] > 0.25 and result["answer"].strip(): answers.append(result["answer"].strip()) except Exception: continue if not answers: return "No detailed answer found in the document." # 🔹 Combine answers into detailed response unique_answers = list(dict.fromkeys(answers)) # remove duplicates detailed_answer = ( "Based on the document, the following points are relevant:\n\n" + "\n- ".join(unique_answers) ) return detailed_answer # ========================= # GRADIO UI # ========================= with gr.Blocks() as demo: gr.Markdown("## 📄 Smart TXT Chatbot (Detailed & Stable)") chat = gr.Chatbot(height=500) box = gr.Textbox(placeholder="Ask a detailed question about the document...") def chat_fn(msg, history): reply = answer_question(msg) history.append((msg, reply)) return "", history box.submit(chat_fn, [box, chat], [box, chat]) if __name__ == "__main__": demo.launch()