Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import asyncio
|
4 |
+
from langchain_core.prompts import PromptTemplate
|
5 |
+
from langchain_community.output_parsers.rail_parser import GuardrailsOutputParser
|
6 |
+
from langchain_community.document_loaders import PyPDFLoader
|
7 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
8 |
+
import google.generativeai as genai
|
9 |
+
from langchain.chains.question_answering import load_qa_chain
|
10 |
+
import torch
|
11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
12 |
+
|
13 |
+
# Gemini PDF QA System
|
14 |
+
async def initialize(file_path, question):
|
15 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
16 |
+
model = genai.GenerativeModel('gemini-pro')
|
17 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
18 |
+
prompt_template = """Answer the question as precise as possible using the provided context. If the answer is
|
19 |
+
not contained in the context, say "answer not available in context" \n\n
|
20 |
+
Context: \n {context}?\n
|
21 |
+
Question: \n {question} \n
|
22 |
+
Answer:
|
23 |
+
"""
|
24 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
25 |
+
if os.path.exists(file_path):
|
26 |
+
pdf_loader = PyPDFLoader(file_path)
|
27 |
+
pages = pdf_loader.load_and_split()
|
28 |
+
context = "\n".join(str(page.page_content) for page in pages[:30])
|
29 |
+
stuff_chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
30 |
+
stuff_answer = await stuff_chain({"input_documents": pages, "question": question, "context": context}, return_only_outputs=True)
|
31 |
+
return stuff_answer['output_text']
|
32 |
+
else:
|
33 |
+
return "Error: Unable to process the document. Please ensure the PDF file is valid."
|
34 |
+
|
35 |
+
async def pdf_qa(file, question):
|
36 |
+
answer = await initialize(file.name, question)
|
37 |
+
return answer
|
38 |
+
|
39 |
+
# Mistral Text Completion
|
40 |
+
def load_mistral_model():
|
41 |
+
model_path = "nvidia/Mistral-NeMo-Minitron-8B-Base"
|
42 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
43 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
44 |
+
dtype = torch.bfloat16
|
45 |
+
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=dtype, device_map=device)
|
46 |
+
return tokenizer, model
|
47 |
+
|
48 |
+
def generate_text(prompt, max_length=50):
|
49 |
+
tokenizer, model = load_mistral_model()
|
50 |
+
inputs = tokenizer.encode(prompt, return_tensors='pt').to(model.device)
|
51 |
+
outputs = model.generate(inputs, max_length=max_length)
|
52 |
+
return tokenizer.decode(outputs[0])
|
53 |
+
|
54 |
+
# Gradio Interface
|
55 |
+
input_file = gr.File(label="Upload PDF File")
|
56 |
+
input_question = gr.Textbox(label="Ask about the document")
|
57 |
+
output_text_gemini = gr.Textbox(label="Answer - GeminiPro")
|
58 |
+
input_prompt = gr.Textbox(label="Enter prompt for text completion")
|
59 |
+
output_text_mistral = gr.Textbox(label="Completed Text - Mistral")
|
60 |
+
|
61 |
+
def pdf_qa_wrapper(file, question):
|
62 |
+
return asyncio.run(pdf_qa(file, question))
|
63 |
+
|
64 |
+
# Create Gradio Interface
|
65 |
+
iface = gr.Interface(
|
66 |
+
fn=[pdf_qa_wrapper, generate_text],
|
67 |
+
inputs=[
|
68 |
+
[input_file, input_question],
|
69 |
+
input_prompt
|
70 |
+
],
|
71 |
+
outputs=[output_text_gemini, output_text_mistral],
|
72 |
+
title="Combined PDF QA and Text Completion System",
|
73 |
+
description="Upload a PDF file to ask questions about its content, or enter a prompt for text completion."
|
74 |
+
)
|
75 |
+
|
76 |
+
iface.launch()
|