aaa59558/bbb
0
1import gradio as gr2from transformers import AutoTokenizer, AutoModelForQuestionAnswering3from sentence_transformers import SentenceTransformer4import faiss5import os6import torch7 8print("๐ข App starting...")9 10# โ
File to load11TXT_FILE = "149.txt"12if not os.path.exists(TXT_FILE):13 raise FileNotFoundError(f"โ File not found: {TXT_FILE}")14 15# โ
Chunking function16def chunk_text(text, max_words=300):17 words = text.split()18 return [" ".join(words[i:i + max_words]) for i in range(0, len(words), max_words)]19 20# โ
Read and chunk the file21with open(TXT_FILE, "r", encoding="utf-8", errors="ignore") as f:22 text = f.read()23chunks = chunk_text(text)24 25# โ
Embedding chunks26print("๐ Creating vector index...")27embedder = SentenceTransformer("all-MiniLM-L6-v2")28embeddings = embedder.encode(chunks)29index = faiss.IndexFlatL2(len(embeddings[0]))30index.add(embeddings)31 32# โ
Load RoBERTa QA model33model_id = "deepset/roberta-base-squad2"34tokenizer = AutoTokenizer.from_pretrained(model_id)35model = AutoModelForQuestionAnswering.from_pretrained(model_id)36 37# โ
Extractive QA function38def extract_answer(question, context):39 inputs = tokenizer(question, context, return_tensors="pt", truncation=True)40 with torch.no_grad():41 outputs = model(**inputs)42 43 start_idx = torch.argmax(outputs.start_logits)44 end_idx = torch.argmax(outputs.end_logits) + 145 46 if start_idx >= end_idx:47 return "โ Couldn't extract a valid answer."48 49 answer = tokenizer.convert_tokens_to_string(50 tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][start_idx:end_idx])51 )52 return answer53 54# โ
Main chat function55def chat_with_bot(user_input):56 q_vec = embedder.encode([user_input])57 distances, indices = index.search(q_vec, k=3)58 59 if distances[0][0] > 1.5:60 return "โ I couldn't find an answer in the document."61 62 # Try multiple chunks to find best answer63 best_answer = ""64 for i in indices[0]:65 answer = extract_answer(user_input, chunks[i])66 if answer and answer.strip() and "โ" not in answer:67 best_answer = answer68 break69 70 return f"๐ง {best_answer}" if best_answer else "โ No confident answer found."71 72# โ
Launch Gradio app73gr.Interface(74 fn=chat_with_bot,75 inputs=gr.Textbox(placeholder="Ask a factual question from 149.txt", lines=2),76 outputs="text",77 title="๐ Extractive QA Chatbot",78 description="Powered by RoBERTa-SQuAD2.0, answers directly from the document using span prediction."79).launch()80 