amritn8 commited on
Commit
e1982cd
Β·
verified Β·
1 Parent(s): ed5f7ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -63
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
- qa_model = BertForQuestionAnswering.from_pretrained("deepset/bert-base-cased-squad2").to(device)
13
- tokenizer = BertTokenizerFast.from_pretrained("deepset/bert-base-cased-squad2")
14
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
15
- whisper_model = whisper.load_model("base")
 
 
 
 
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.read().decode("utf-8")
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
- with gr.Blocks() as demo:
57
- gr.Markdown("# πŸŽ™οΈπŸ“„ LexPilot: Voice + Document Q&A Assistant")
58
- gr.Markdown("Upload a document or paste content. Ask questions by typing or using your voice.")
59
-
60
- with gr.Tab("Question Answering"):
61
- with gr.Row():
62
- uploaded_file = gr.File(label="Upload .pdf / .docx / .txt", file_types=[".pdf", ".docx", ".txt"])
63
- pasted_text = gr.Textbox(label="Paste text manually", lines=10)
64
- with gr.Row():
65
- typed_question = gr.Textbox(label="Type your question")
66
- audio_input = gr.Audio(source="microphone",type="filepath", label="Or speak your question")
67
- qa_btn = gr.Button("Get Answer")
68
- qa_output = gr.Textbox(label="Answer and Confidence", lines=3)
69
-
70
- def handle_qa(file, text, typed, audio):
71
- context = ""
72
- if file:
73
- context = extract_text(file)
74
- elif text:
75
- context = text
76
- else:
77
- return "❗ Please upload or paste content."
78
-
79
- if typed:
80
- question = typed
81
- elif audio:
82
- question = transcribe(audio)
83
- else:
84
- return "❗ Please speak or type a question."
85
-
86
- return ask_question(question, context)
87
-
88
- qa_btn.click(handle_qa, inputs=[uploaded_file, pasted_text, typed_question, audio_input], outputs=qa_output)
89
-
90
- with gr.Tab("Summarization"):
91
- with gr.Row():
92
- sum_file = gr.File(label="Upload .pdf / .docx / .txt", file_types=[".pdf", ".docx", ".txt"])
93
- sum_text = gr.Textbox(label="Or paste content", lines=10)
94
- sum_btn = gr.Button("Summarize")
95
- sum_output = gr.Textbox(label="Summary", lines=4)
96
-
97
- def handle_summary(file, text):
98
- if file:
99
- context = extract_text(file)
100
- elif text:
101
- context = text
102
- else:
103
- return "❗ Please upload or paste content to summarize."
104
- return summarize_text(context)
105
-
106
- sum_btn.click(handle_summary, inputs=[sum_file, sum_text], outputs=sum_output)
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)