Rohit1412 commited on
Commit
4027709
·
verified ·
1 Parent(s): 0cb9fb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -34
app.py CHANGED
@@ -2,8 +2,6 @@ import gradio as gr
2
  import torch
3
  from sentence_transformers import SentenceTransformer, util
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
- from langchain.chains import LLMChain
6
- from langchain.prompts import PromptTemplate
7
  import PyPDF2
8
  import os
9
  import time
@@ -21,15 +19,6 @@ gen_model = AutoModelForCausalLM.from_pretrained("microsoft/phi-1", torch_dtype=
21
  # Cache for document embeddings
22
  embedding_cache = {}
23
 
24
- # LangChain wrapper for Phi-1
25
- class Phi1LLM:
26
- def __call__(self, prompt, **kwargs):
27
- inputs = gen_tokenizer(prompt, return_tensors="pt")
28
- outputs = gen_model.generate(**inputs, max_new_tokens=150, num_beams=2)
29
- return gen_tokenizer.decode(outputs[0], skip_special_tokens=True)
30
-
31
- phi1_llm = Phi1LLM()
32
-
33
  def extract_text_from_pdf(pdf_file):
34
  """Extract text from a PDF file, returning a list of page texts."""
35
  pages = []
@@ -74,8 +63,14 @@ def get_document_embeddings(documents):
74
  embeddings.append(emb)
75
  return torch.stack(embeddings)
76
 
 
 
 
 
 
 
77
  def rag_pipeline(question, pdf_files):
78
- """RAG pipeline with multi-step thinking using Phi-1 and LangChain."""
79
  start_time = time.time()
80
  documents = []
81
 
@@ -114,31 +109,23 @@ def rag_pipeline(question, pdf_files):
114
  logger.info(f"Retrieved context:\n{retrieved_context}")
115
 
116
  # Step 1: Initial Answer
117
- initial_prompt = PromptTemplate(
118
- input_variables=["context", "question"],
119
- template=(
120
- "Using the following context, provide a concise answer to the question:\n\n"
121
- "Context:\n{context}\n\n"
122
- "Question: {question}\n\n"
123
- "Answer:"
124
- )
125
  )
126
- initial_chain = LLMChain(llm=phi1_llm, prompt=initial_prompt)
127
- initial_answer = initial_chain.run(context=retrieved_context, question=question)
128
 
129
  # Step 2: Refine Answer
130
- refine_prompt = PromptTemplate(
131
- input_variables=["context", "question", "initial_answer"],
132
- template=(
133
- "Given the context and initial answer, refine and improve the response to the question:\n\n"
134
- "Context:\n{context}\n\n"
135
- "Question: {question}\n\n"
136
- "Initial Answer: {initial_answer}\n\n"
137
- "Refined Answer:"
138
- )
139
  )
140
- refine_chain = LLMChain(llm=phi1_llm, prompt=refine_prompt)
141
- refined_answer = refine_chain.run(context=retrieved_context, question=question, initial_answer=initial_answer)
142
 
143
  logger.info(f"Initial answer: {initial_answer}")
144
  logger.info(f"Refined answer: {refined_answer}")
@@ -162,4 +149,4 @@ with gr.Blocks() as demo:
162
 
163
  submit_button.click(fn=rag_pipeline, inputs=[question_input, pdf_input], outputs=response_output)
164
 
165
- demo.launch(share=True, debug=True)
 
2
  import torch
3
  from sentence_transformers import SentenceTransformer, util
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
5
  import PyPDF2
6
  import os
7
  import time
 
19
  # Cache for document embeddings
20
  embedding_cache = {}
21
 
 
 
 
 
 
 
 
 
 
22
  def extract_text_from_pdf(pdf_file):
23
  """Extract text from a PDF file, returning a list of page texts."""
24
  pages = []
 
63
  embeddings.append(emb)
64
  return torch.stack(embeddings)
65
 
66
+ def generate_response(prompt):
67
+ """Helper function to generate text with Phi-1."""
68
+ inputs = gen_tokenizer(prompt, return_tensors="pt")
69
+ outputs = gen_model.generate(**inputs, max_new_tokens=150, num_beams=2)
70
+ return gen_tokenizer.decode(outputs[0], skip_special_tokens=True)
71
+
72
  def rag_pipeline(question, pdf_files):
73
+ """RAG pipeline with multi-step thinking using Phi-1."""
74
  start_time = time.time()
75
  documents = []
76
 
 
109
  logger.info(f"Retrieved context:\n{retrieved_context}")
110
 
111
  # Step 1: Initial Answer
112
+ initial_prompt = (
113
+ f"Using the following context, provide a concise answer to the question:\n\n"
114
+ f"Context:\n{retrieved_context}\n\n"
115
+ f"Question: {question}\n\n"
116
+ f"Answer:"
 
 
 
117
  )
118
+ initial_answer = generate_response(initial_prompt)
 
119
 
120
  # Step 2: Refine Answer
121
+ refine_prompt = (
122
+ f"Given the context and initial answer, refine and improve the response to the question:\n\n"
123
+ f"Context:\n{retrieved_context}\n\n"
124
+ f"Question: {question}\n\n"
125
+ f"Initial Answer: {initial_answer}\n\n"
126
+ f"Refined Answer:"
 
 
 
127
  )
128
+ refined_answer = generate_response(refine_prompt)
 
129
 
130
  logger.info(f"Initial answer: {initial_answer}")
131
  logger.info(f"Refined answer: {refined_answer}")
 
149
 
150
  submit_button.click(fn=rag_pipeline, inputs=[question_input, pdf_input], outputs=response_output)
151
 
152
+ demo.launch()