nsultan5 commited on
Commit
6292af0
·
verified ·
1 Parent(s): 58ad61d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -5
app.py CHANGED
@@ -14,6 +14,8 @@ from langchain.schema import LLMResult
14
  from langchain.llms.base import LLM
15
  import torch
16
  import os
 
 
17
 
18
  # --------------------------
19
  # Load Quantized LLaMA 2 via AutoGPTQ
@@ -61,8 +63,6 @@ def generate_text(prompt):
61
  class CustomLlamaLLM(LLM):
62
  def _call(self, prompt: str, stop=None) -> str:
63
  output = generate_text(prompt)
64
- # Return only the generated part after prompt
65
- # Assume output contains prompt + generated text
66
  generated = output.split("[/INST]")[-1].strip()
67
  return generated
68
 
@@ -125,6 +125,40 @@ def upload_and_index_pdf(pdf_file):
125
  vector_db.persist()
126
  return f"Uploaded and indexed: {os.path.basename(pdf_file.name)}"
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  # --------------------------
129
  # Gradio UI
130
  # --------------------------
@@ -160,11 +194,8 @@ with gr.Blocks(title="GenAI Customer Support") as demo:
160
  generated_questions = gr.Textbox(label="Generated Sample Questions", lines=10)
161
  gen_qs_button = gr.Button("Generate Questions")
162
  gen_qs_button.click(fn=generate_test_questions_from_pdf, inputs=pdf_file_for_qs, outputs=generated_questions)
163
-
164
 
165
  # --------------------------
166
  # Launch
167
  # --------------------------
168
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
169
-
170
-
 
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
 
63
  class CustomLlamaLLM(LLM):
64
  def _call(self, prompt: str, stop=None) -> str:
65
  output = generate_text(prompt)
 
 
66
  generated = output.split("[/INST]")[-1].strip()
67
  return generated
68
 
 
125
  vector_db.persist()
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
164
  # --------------------------
 
194
  generated_questions = gr.Textbox(label="Generated Sample Questions", lines=10)
195
  gen_qs_button = gr.Button("Generate Questions")
196
  gen_qs_button.click(fn=generate_test_questions_from_pdf, inputs=pdf_file_for_qs, outputs=generated_questions)
 
197
 
198
  # --------------------------
199
  # Launch
200
  # --------------------------
201
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)