nsultan5 commited on
Commit
44ecd07
·
verified ·
1 Parent(s): 9466e15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -42
app.py CHANGED
@@ -14,8 +14,6 @@ from langchain.schema import LLMResult
14
  from langchain.llms.base import LLM
15
  import torch
16
  import os
17
- import random
18
- import PyPDF2
19
 
20
  # --------------------------
21
  # Load Quantized LLaMA 2 via AutoGPTQ
@@ -43,17 +41,28 @@ model = AutoGPTQForCausalLM.from_quantized(
43
  token=hf_token
44
  )
45
 
 
 
 
 
46
  # Manual text generation function
 
47
  def generate_text(prompt):
48
  inputs = tokenizer(prompt, return_tensors="pt").to(device)
49
- generation_output = model.generate(
50
- input_ids=inputs.input_ids,
51
- attention_mask=inputs.attention_mask,
52
- max_new_tokens=256,
53
- do_sample=True,
54
- temperature=0.7,
55
- top_p=0.9,
56
- )
 
 
 
 
 
 
57
  decoded = tokenizer.decode(generation_output[0], skip_special_tokens=True)
58
  return decoded
59
 
@@ -95,7 +104,8 @@ whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-
95
 
96
  def transcribe_audio(audio):
97
  audio_input = whisper_processor(audio["array"], sampling_rate=audio["sampling_rate"], return_tensors="pt")
98
- result = whisper_model.generate(**audio_input)
 
99
  return whisper_processor.batch_decode(result, skip_special_tokens=True)[0]
100
 
101
  # --------------------------
@@ -104,8 +114,7 @@ def transcribe_audio(audio):
104
  def handle_user_input(text):
105
  prompt = f"<s>[INST] {text} [/INST]"
106
  response = generate_text(prompt)
107
- response = response.split("[/INST]")[-1].strip()
108
- return response
109
 
110
  def handle_pdf_query(text):
111
  result = qa_chain(text)
@@ -126,38 +135,13 @@ def upload_and_index_pdf(pdf_file):
126
  return f"Uploaded and indexed: {os.path.basename(pdf_file.name)}"
127
 
128
  # --------------------------
129
- # Generate Sample Questions from PDF
130
  # --------------------------
131
  def generate_test_questions_from_pdf(pdf_file):
 
132
  if pdf_file is None:
133
- return "No file uploaded."
134
-
135
- # Read text from PDF
136
- pdf_reader = PyPDF2.PdfReader(pdf_file.name)
137
- text = ""
138
- for page in pdf_reader.pages:
139
- text += page.extract_text() + " "
140
-
141
- # Split text into sentences
142
- sentences = [s.strip() for s in text.split(".") if s.strip()]
143
- if not sentences:
144
- return "No extractable text found in PDF."
145
-
146
- # Generate sample questions (randomly)
147
- questions = []
148
- templates = [
149
- "Explain the following: {}?",
150
- "What is the main point of: {}?",
151
- "Summarize: {}",
152
- "Why is {} important?"
153
- ]
154
-
155
- for _ in range(min(10, len(sentences))):
156
- sentence = random.choice(sentences)
157
- template = random.choice(templates)
158
- questions.append(template.format(sentence[:100])) # limit to 100 chars
159
-
160
- return "\n".join(questions)
161
 
162
  # --------------------------
163
  # Gradio UI
 
14
  from langchain.llms.base import LLM
15
  import torch
16
  import os
 
 
17
 
18
  # --------------------------
19
  # Load Quantized LLaMA 2 via AutoGPTQ
 
41
  token=hf_token
42
  )
43
 
44
+ model.to(device)
45
+ model.eval() # Ensure evaluation mode
46
+
47
+ # --------------------------
48
  # Manual text generation function
49
+ # --------------------------
50
  def generate_text(prompt):
51
  inputs = tokenizer(prompt, return_tensors="pt").to(device)
52
+
53
+ # Ensure input types are compatible
54
+ inputs.input_ids = inputs.input_ids.to(torch.int64)
55
+ inputs.attention_mask = inputs.attention_mask.to(torch.int64)
56
+
57
+ with torch.no_grad():
58
+ generation_output = model.generate(
59
+ input_ids=inputs.input_ids,
60
+ attention_mask=inputs.attention_mask,
61
+ max_new_tokens=256,
62
+ do_sample=True,
63
+ temperature=0.7,
64
+ top_p=0.9,
65
+ )
66
  decoded = tokenizer.decode(generation_output[0], skip_special_tokens=True)
67
  return decoded
68
 
 
104
 
105
  def transcribe_audio(audio):
106
  audio_input = whisper_processor(audio["array"], sampling_rate=audio["sampling_rate"], return_tensors="pt")
107
+ with torch.no_grad():
108
+ result = whisper_model.generate(**audio_input)
109
  return whisper_processor.batch_decode(result, skip_special_tokens=True)[0]
110
 
111
  # --------------------------
 
114
  def handle_user_input(text):
115
  prompt = f"<s>[INST] {text} [/INST]"
116
  response = generate_text(prompt)
117
+ return response.split("[/INST]")[-1].strip()
 
118
 
119
  def handle_pdf_query(text):
120
  result = qa_chain(text)
 
135
  return f"Uploaded and indexed: {os.path.basename(pdf_file.name)}"
136
 
137
  # --------------------------
138
+ # Placeholder for test question generation
139
  # --------------------------
140
  def generate_test_questions_from_pdf(pdf_file):
141
+ # This is a placeholder — implement your own question generation logic
142
  if pdf_file is None:
143
+ return "No PDF uploaded."
144
+ return "Sample Question 1\nSample Question 2\nSample Question 3"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  # --------------------------
147
  # Gradio UI