Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,23 @@ import os
|
|
| 2 |
import torch
|
| 3 |
import whisper
|
| 4 |
import PyPDF2
|
| 5 |
-
import gradio as gr
|
| 6 |
from transformers import BertTokenizerFast, BertForQuestionAnswering, pipeline
|
| 7 |
from torch.nn.functional import softmax
|
| 8 |
from docx import Document
|
|
|
|
| 9 |
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 17 |
|
| 18 |
def extract_text(file_obj):
|
| 19 |
ext = os.path.splitext(file_obj.name)[1].lower()
|
|
@@ -24,10 +29,9 @@ def extract_text(file_obj):
|
|
| 24 |
doc = Document(file_obj)
|
| 25 |
return "\n".join([p.text for p in doc.paragraphs])
|
| 26 |
elif ext == ".txt":
|
| 27 |
-
return file_obj.
|
| 28 |
return ""
|
| 29 |
|
| 30 |
-
|
| 31 |
def summarize_text(text):
|
| 32 |
if len(text) < 50:
|
| 33 |
return "Text too short to summarize."
|
|
@@ -36,7 +40,6 @@ def summarize_text(text):
|
|
| 36 |
summary = summarizer(text, max_length=120, min_length=30, do_sample=False)
|
| 37 |
return summary[0]['summary_text']
|
| 38 |
|
| 39 |
-
|
| 40 |
def ask_question(question, context):
|
| 41 |
inputs = tokenizer.encode_plus(question, context, return_tensors="pt", truncation=True, max_length=512).to(device)
|
| 42 |
with torch.no_grad():
|
|
@@ -47,62 +50,60 @@ def ask_question(question, context):
|
|
| 47 |
answer = tokenizer.decode(inputs["input_ids"][0][start_idx:end_idx])
|
| 48 |
return f"Answer: {answer.strip()}\nConfidence: {round(score.item()*100, 2)}%"
|
| 49 |
|
| 50 |
-
|
| 51 |
def transcribe(audio_path):
|
| 52 |
result = whisper_model.transcribe(audio_path)
|
| 53 |
return result["text"]
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
demo.launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
import whisper
|
| 4 |
import PyPDF2
|
|
|
|
| 5 |
from transformers import BertTokenizerFast, BertForQuestionAnswering, pipeline
|
| 6 |
from torch.nn.functional import softmax
|
| 7 |
from docx import Document
|
| 8 |
+
import streamlit as st
|
| 9 |
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
|
| 12 |
+
# Load models once
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def load_models():
|
| 15 |
+
qa_model = BertForQuestionAnswering.from_pretrained("deepset/bert-base-cased-squad2").to(device)
|
| 16 |
+
tokenizer = BertTokenizerFast.from_pretrained("deepset/bert-base-cased-squad2")
|
| 17 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 18 |
+
whisper_model = whisper.load_model("base")
|
| 19 |
+
return qa_model, tokenizer, summarizer, whisper_model
|
| 20 |
|
| 21 |
+
qa_model, tokenizer, summarizer, whisper_model = load_models()
|
| 22 |
|
| 23 |
def extract_text(file_obj):
|
| 24 |
ext = os.path.splitext(file_obj.name)[1].lower()
|
|
|
|
| 29 |
doc = Document(file_obj)
|
| 30 |
return "\n".join([p.text for p in doc.paragraphs])
|
| 31 |
elif ext == ".txt":
|
| 32 |
+
return file_obj.getvalue().decode("utf-8")
|
| 33 |
return ""
|
| 34 |
|
|
|
|
| 35 |
def summarize_text(text):
|
| 36 |
if len(text) < 50:
|
| 37 |
return "Text too short to summarize."
|
|
|
|
| 40 |
summary = summarizer(text, max_length=120, min_length=30, do_sample=False)
|
| 41 |
return summary[0]['summary_text']
|
| 42 |
|
|
|
|
| 43 |
def ask_question(question, context):
|
| 44 |
inputs = tokenizer.encode_plus(question, context, return_tensors="pt", truncation=True, max_length=512).to(device)
|
| 45 |
with torch.no_grad():
|
|
|
|
| 50 |
answer = tokenizer.decode(inputs["input_ids"][0][start_idx:end_idx])
|
| 51 |
return f"Answer: {answer.strip()}\nConfidence: {round(score.item()*100, 2)}%"
|
| 52 |
|
|
|
|
| 53 |
def transcribe(audio_path):
|
| 54 |
result = whisper_model.transcribe(audio_path)
|
| 55 |
return result["text"]
|
| 56 |
|
| 57 |
+
st.title("ποΈπ LexPilot: Voice + Document Q&A Assistant")
|
| 58 |
+
st.write("Upload a document or paste content. Ask questions by typing or speaking.")
|
| 59 |
+
|
| 60 |
+
tab = st.tabs(["Question Answering", "Summarization"])
|
| 61 |
+
|
| 62 |
+
with tab[0]:
|
| 63 |
+
uploaded_file = st.file_uploader("Upload .pdf / .docx / .txt", type=["pdf", "docx", "txt"])
|
| 64 |
+
pasted_text = st.text_area("Or paste text manually", height=150)
|
| 65 |
+
|
| 66 |
+
typed_question = st.text_input("Type your question")
|
| 67 |
+
audio_input = st.file_uploader("Or upload audio file (wav, mp3, m4a)", type=["wav", "mp3", "m4a"])
|
| 68 |
+
|
| 69 |
+
if st.button("Get Answer"):
|
| 70 |
+
context = ""
|
| 71 |
+
if uploaded_file:
|
| 72 |
+
context = extract_text(uploaded_file)
|
| 73 |
+
elif pasted_text.strip():
|
| 74 |
+
context = pasted_text.strip()
|
| 75 |
+
else:
|
| 76 |
+
st.warning("β Please upload or paste content.")
|
| 77 |
+
st.stop()
|
| 78 |
+
|
| 79 |
+
if typed_question.strip():
|
| 80 |
+
question = typed_question.strip()
|
| 81 |
+
elif audio_input:
|
| 82 |
+
# Save audio temporarily
|
| 83 |
+
with open("temp_audio", "wb") as f:
|
| 84 |
+
f.write(audio_input.getbuffer())
|
| 85 |
+
question = transcribe("temp_audio")
|
| 86 |
+
st.write(f"Transcribed question: {question}")
|
| 87 |
+
else:
|
| 88 |
+
st.warning("β Please type or upload an audio question.")
|
| 89 |
+
st.stop()
|
| 90 |
+
|
| 91 |
+
answer = ask_question(question, context)
|
| 92 |
+
st.text_area("Answer and Confidence", value=answer, height=100)
|
| 93 |
+
|
| 94 |
+
with tab[1]:
|
| 95 |
+
sum_file = st.file_uploader("Upload .pdf / .docx / .txt to summarize", type=["pdf", "docx", "txt"])
|
| 96 |
+
sum_text = st.text_area("Or paste content to summarize", height=150)
|
| 97 |
+
|
| 98 |
+
if st.button("Summarize"):
|
| 99 |
+
context = ""
|
| 100 |
+
if sum_file:
|
| 101 |
+
context = extract_text(sum_file)
|
| 102 |
+
elif sum_text.strip():
|
| 103 |
+
context = sum_text.strip()
|
| 104 |
+
else:
|
| 105 |
+
st.warning("β Please upload or paste content to summarize.")
|
| 106 |
+
st.stop()
|
| 107 |
+
|
| 108 |
+
summary = summarize_text(context)
|
| 109 |
+
st.text_area("Summary", value=summary, height=150)
|
|
|