Spaces:
Sleeping
Sleeping
Roberta2024
commited on
Commit
•
959d70e
1
Parent(s):
ec8e07e
Update app.py
Browse files
app.py
CHANGED
@@ -3,50 +3,59 @@ import gradio as gr
|
|
3 |
import asyncio
|
4 |
from langchain_core.prompts import PromptTemplate
|
5 |
from langchain_community.document_loaders import PyPDFLoader
|
|
|
|
|
|
|
6 |
import torch
|
7 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
8 |
|
|
|
|
|
|
|
9 |
# Load Mistral model
|
10 |
model_path = "nvidia/Mistral-NeMo-Minitron-8B-Base"
|
11 |
-
|
12 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
13 |
dtype = torch.bfloat16
|
14 |
-
|
15 |
|
16 |
async def initialize(file_path, question):
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
19 |
-
|
20 |
if os.path.exists(file_path):
|
21 |
pdf_loader = PyPDFLoader(file_path)
|
22 |
pages = pdf_loader.load_and_split()
|
23 |
context = "\n".join(str(page.page_content) for page in pages[:30])
|
|
|
|
|
|
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
# Generate the output
|
30 |
with torch.no_grad():
|
31 |
-
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
return answer
|
36 |
else:
|
37 |
return "Error: Unable to process the document. Please ensure the PDF file is valid."
|
38 |
|
39 |
# Define Gradio Interface
|
40 |
input_file = gr.File(label="Upload PDF File")
|
41 |
input_question = gr.Textbox(label="Ask about the document")
|
42 |
-
output_text = gr.Textbox(label="Answer - Mistral
|
43 |
|
44 |
-
def pdf_qa(file, question):
|
45 |
-
|
46 |
-
return "Please upload a PDF file first."
|
47 |
-
|
48 |
-
loop = asyncio.get_event_loop()
|
49 |
-
answer = loop.run_until_complete(initialize(file.name, question))
|
50 |
return answer
|
51 |
|
52 |
# Create Gradio Interface
|
@@ -54,6 +63,6 @@ gr.Interface(
|
|
54 |
fn=pdf_qa,
|
55 |
inputs=[input_file, input_question],
|
56 |
outputs=output_text,
|
57 |
-
title="RAG Knowledge Retrieval using Mistral Model",
|
58 |
description="Upload a PDF file and ask questions about the content."
|
59 |
).launch()
|
|
|
3 |
import asyncio
|
4 |
from langchain_core.prompts import PromptTemplate
|
5 |
from langchain_community.document_loaders import PyPDFLoader
|
6 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
7 |
+
import google.generativeai as genai
|
8 |
+
from langchain.chains.question_answering import load_qa_chain
|
9 |
import torch
|
10 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
11 |
|
12 |
+
# Configure Gemini API
|
13 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
+
|
15 |
# Load Mistral model
|
16 |
model_path = "nvidia/Mistral-NeMo-Minitron-8B-Base"
|
17 |
+
mistral_tokenizer = AutoTokenizer.from_pretrained(model_path)
|
18 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
19 |
dtype = torch.bfloat16
|
20 |
+
mistral_model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=dtype, device_map=device)
|
21 |
|
22 |
async def initialize(file_path, question):
|
23 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
24 |
+
prompt_template = """Answer the question as precise as possible using the provided context. If the answer is
|
25 |
+
not contained in the context, say "answer not available in context" \n\n
|
26 |
+
Context: \n {context}?\n
|
27 |
+
Question: \n {question} \n
|
28 |
+
Answer:
|
29 |
+
"""
|
30 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
31 |
+
|
32 |
if os.path.exists(file_path):
|
33 |
pdf_loader = PyPDFLoader(file_path)
|
34 |
pages = pdf_loader.load_and_split()
|
35 |
context = "\n".join(str(page.page_content) for page in pages[:30])
|
36 |
+
stuff_chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
37 |
+
stuff_answer = await stuff_chain.acall({"input_documents": pages, "question": question, "context": context}, return_only_outputs=True)
|
38 |
+
gemini_answer = stuff_answer['output_text']
|
39 |
|
40 |
+
# Use Mistral model for additional text generation
|
41 |
+
mistral_prompt = f"Based on this answer: {gemini_answer}\nGenerate a follow-up question:"
|
42 |
+
mistral_inputs = mistral_tokenizer.encode(mistral_prompt, return_tensors='pt').to(device)
|
|
|
|
|
43 |
with torch.no_grad():
|
44 |
+
mistral_outputs = mistral_model.generate(mistral_inputs, max_length=50)
|
45 |
+
mistral_output = mistral_tokenizer.decode(mistral_outputs[0], skip_special_tokens=True)
|
46 |
|
47 |
+
combined_output = f"Gemini Answer: {gemini_answer}\n\nMistral Follow-up: {mistral_output}"
|
48 |
+
return combined_output
|
|
|
49 |
else:
|
50 |
return "Error: Unable to process the document. Please ensure the PDF file is valid."
|
51 |
|
52 |
# Define Gradio Interface
|
53 |
input_file = gr.File(label="Upload PDF File")
|
54 |
input_question = gr.Textbox(label="Ask about the document")
|
55 |
+
output_text = gr.Textbox(label="Answer - Combined Gemini and Mistral")
|
56 |
|
57 |
+
async def pdf_qa(file, question):
|
58 |
+
answer = await initialize(file.name, question)
|
|
|
|
|
|
|
|
|
59 |
return answer
|
60 |
|
61 |
# Create Gradio Interface
|
|
|
63 |
fn=pdf_qa,
|
64 |
inputs=[input_file, input_question],
|
65 |
outputs=output_text,
|
66 |
+
title="RAG Knowledge Retrieval using Gemini API and Mistral Model",
|
67 |
description="Upload a PDF file and ask questions about the content."
|
68 |
).launch()
|