Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
|
|
3 |
from langchain import PromptTemplate
|
4 |
from langchain.chains.question_answering import load_qa_chain
|
5 |
from langchain.document_loaders import PyPDFLoader
|
@@ -7,9 +8,8 @@ from langchain_google_genai import ChatGoogleGenerativeAI
|
|
7 |
import google.generativeai as genai
|
8 |
|
9 |
|
10 |
-
|
11 |
# Fungsi untuk inisialisasi
|
12 |
-
def initialize(file_path, question):
|
13 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
model = genai.GenerativeModel('gemini-pro')
|
15 |
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
@@ -25,20 +25,21 @@ def initialize(file_path, question):
|
|
25 |
pages = pdf_loader.load_and_split()
|
26 |
context = "\n".join(str(page.page_content) for page in pages[:30])
|
27 |
stuff_chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
28 |
-
stuff_answer = stuff_chain({"input_documents": pages, "question": question, "context": context}, return_only_outputs=True)
|
29 |
return stuff_answer['output_text']
|
30 |
else:
|
31 |
return "Error: Unable to process the document. Please ensure the PDF file is valid."
|
32 |
|
33 |
# Define Gradio Interface
|
34 |
-
input_file = gr.File(label="Upload PDF File")
|
35 |
-
input_question = gr.Textbox(label="Ask about the document")
|
36 |
-
output_text = gr.Textbox(label="Answer - GeminiPro")
|
37 |
|
38 |
-
def pdf_qa(file, question):
|
39 |
-
answer = initialize(file.name, question)
|
40 |
return answer
|
41 |
|
42 |
# Create Gradio Interface
|
43 |
gr.Interface(fn=pdf_qa, inputs=[input_file, input_question], outputs=output_text, title="PDF Question Answering System", description="Upload a PDF file and ask questions about the content.").launch()
|
44 |
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
import asyncio
|
4 |
from langchain import PromptTemplate
|
5 |
from langchain.chains.question_answering import load_qa_chain
|
6 |
from langchain.document_loaders import PyPDFLoader
|
|
|
8 |
import google.generativeai as genai
|
9 |
|
10 |
|
|
|
11 |
# Fungsi untuk inisialisasi
|
12 |
+
async def initialize(file_path, question):
|
13 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
model = genai.GenerativeModel('gemini-pro')
|
15 |
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
|
|
25 |
pages = pdf_loader.load_and_split()
|
26 |
context = "\n".join(str(page.page_content) for page in pages[:30])
|
27 |
stuff_chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
28 |
+
stuff_answer = await stuff_chain({"input_documents": pages, "question": question, "context": context}, return_only_outputs=True)
|
29 |
return stuff_answer['output_text']
|
30 |
else:
|
31 |
return "Error: Unable to process the document. Please ensure the PDF file is valid."
|
32 |
|
33 |
# Define Gradio Interface
|
34 |
+
input_file = gr.inputs.File(label="Upload PDF File")
|
35 |
+
input_question = gr.inputs.Textbox(label="Ask about the document")
|
36 |
+
output_text = gr.outputs.Textbox(label="Answer - GeminiPro")
|
37 |
|
38 |
+
async def pdf_qa(file, question):
|
39 |
+
answer = await initialize(file.name, question)
|
40 |
return answer
|
41 |
|
42 |
# Create Gradio Interface
|
43 |
gr.Interface(fn=pdf_qa, inputs=[input_file, input_question], outputs=output_text, title="PDF Question Answering System", description="Upload a PDF file and ask questions about the content.").launch()
|
44 |
|
45 |
+
|