Commit
·
f020c4b
1
Parent(s):
a5d7bc8
first commit
Browse files- .gitignore +149 -0
- app.py +67 -0
- downloaded_paper.pdf +0 -0
- requirements.txt +6 -0
.gitignore
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Local folder
|
10 |
+
local_folder
|
11 |
+
project_demo
|
12 |
+
project_demo/
|
13 |
+
logs/
|
14 |
+
local_folder/
|
15 |
+
/demo.py
|
16 |
+
demo.py
|
17 |
+
runs/
|
18 |
+
downloaded_paper.pdf/
|
19 |
+
|
20 |
+
# Large folders
|
21 |
+
weights/
|
22 |
+
videos/
|
23 |
+
images/
|
24 |
+
|
25 |
+
|
26 |
+
# Distribution / packaging
|
27 |
+
.Python
|
28 |
+
build/
|
29 |
+
develop-eggs/
|
30 |
+
dist/
|
31 |
+
downloads/
|
32 |
+
eggs/
|
33 |
+
.eggs/
|
34 |
+
lib/
|
35 |
+
lib64/
|
36 |
+
parts/
|
37 |
+
sdist/
|
38 |
+
var/
|
39 |
+
wheels/
|
40 |
+
pip-wheel-metadata/
|
41 |
+
share/python-wheels/
|
42 |
+
*.egg-info/
|
43 |
+
.installed.cfg
|
44 |
+
*.egg
|
45 |
+
MANIFEST
|
46 |
+
|
47 |
+
# PyInstaller
|
48 |
+
# Usually these files are written by a python script from a template
|
49 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
50 |
+
*.manifest
|
51 |
+
*.spec
|
52 |
+
|
53 |
+
.vscode
|
54 |
+
|
55 |
+
# Installer logs
|
56 |
+
pip-log.txt
|
57 |
+
pip-delete-this-directory.txt
|
58 |
+
|
59 |
+
# Unit test / coverage reports
|
60 |
+
htmlcov/
|
61 |
+
.tox/
|
62 |
+
.nox/
|
63 |
+
.coverage
|
64 |
+
.coverage.*
|
65 |
+
.cache
|
66 |
+
nosetests.xml
|
67 |
+
coverage.xml
|
68 |
+
*.cover
|
69 |
+
*.py,cover
|
70 |
+
.hypothesis/
|
71 |
+
.pytest_cache/
|
72 |
+
|
73 |
+
# Translations
|
74 |
+
*.mo
|
75 |
+
*.pot
|
76 |
+
|
77 |
+
# Django stuff:
|
78 |
+
*.log
|
79 |
+
local_settings.py
|
80 |
+
db.sqlite3
|
81 |
+
db.sqlite3-journal
|
82 |
+
|
83 |
+
# Flask stuff:
|
84 |
+
instance/
|
85 |
+
.webassets-cache
|
86 |
+
|
87 |
+
# Scrapy stuff:
|
88 |
+
.scrapy
|
89 |
+
|
90 |
+
# Sphinx documentation
|
91 |
+
docs/_build/
|
92 |
+
|
93 |
+
# PyBuilder
|
94 |
+
target/
|
95 |
+
|
96 |
+
# Jupyter Notebook
|
97 |
+
.ipynb_checkpoints
|
98 |
+
|
99 |
+
# IPython
|
100 |
+
profile_default/
|
101 |
+
ipython_config.py
|
102 |
+
|
103 |
+
# pyenv
|
104 |
+
.python-version
|
105 |
+
|
106 |
+
# pipenv
|
107 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
108 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
109 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
110 |
+
# install all needed dependencies.
|
111 |
+
#Pipfile.lock
|
112 |
+
|
113 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
114 |
+
__pypackages__/
|
115 |
+
|
116 |
+
# Celery stuff
|
117 |
+
celerybeat-schedule
|
118 |
+
celerybeat.pid
|
119 |
+
|
120 |
+
# SageMath parsed files
|
121 |
+
*.sage.py
|
122 |
+
|
123 |
+
# Environments
|
124 |
+
.env
|
125 |
+
.venv
|
126 |
+
env/
|
127 |
+
venv/
|
128 |
+
venv_/
|
129 |
+
ENV/
|
130 |
+
env.bak/
|
131 |
+
venv.bak/
|
132 |
+
|
133 |
+
# Spyder project settings
|
134 |
+
.spyderproject
|
135 |
+
.spyproject
|
136 |
+
|
137 |
+
# Rope project settings
|
138 |
+
.ropeproject
|
139 |
+
|
140 |
+
# mkdocs documentation
|
141 |
+
/site
|
142 |
+
|
143 |
+
# mypy
|
144 |
+
.mypy_cache/
|
145 |
+
.dmypy.json
|
146 |
+
dmypy.json
|
147 |
+
|
148 |
+
# Pyre type checker
|
149 |
+
.pyre/
|
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import urllib
|
2 |
+
import warnings
|
3 |
+
from pathlib import Path
|
4 |
+
import os
|
5 |
+
import gradio as gr
|
6 |
+
from langchain import PromptTemplate
|
7 |
+
from langchain.chains.question_answering import load_qa_chain
|
8 |
+
from langchain.document_loaders import PyPDFLoader
|
9 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
10 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
11 |
+
import google.generativeai as genai
|
12 |
+
import pandas as pd
|
13 |
+
|
14 |
+
from dotenv import load_dotenv
|
15 |
+
load_dotenv() # take environment variables from .env.
|
16 |
+
|
17 |
+
|
18 |
+
# Fungsi untuk inisialisasi
|
19 |
+
def initialize(link, question):
|
20 |
+
|
21 |
+
# Konfigurasikan kunci API
|
22 |
+
os.getenv("GOOGLE_API_KEY")
|
23 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
24 |
+
model = genai.GenerativeModel('gemini-pro')
|
25 |
+
|
26 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
27 |
+
|
28 |
+
prompt_template = """Answer the question as precise as possible using the provided context. If the answer is
|
29 |
+
not contained in the context, say "answer not available in context" \n\n
|
30 |
+
Context: \n {context}?\n
|
31 |
+
Question: \n {question} \n
|
32 |
+
Answer:
|
33 |
+
"""
|
34 |
+
|
35 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
36 |
+
|
37 |
+
# Download PDF dari URL yang diberikan
|
38 |
+
pdf_file = "downloaded_paper.pdf"
|
39 |
+
urllib.request.urlretrieve(link, pdf_file)
|
40 |
+
|
41 |
+
# Load the PDF
|
42 |
+
pdf_loader = PyPDFLoader(pdf_file)
|
43 |
+
pages = pdf_loader.load_and_split()
|
44 |
+
|
45 |
+
# Process the file content and use it as the context
|
46 |
+
context = "\n".join(str(page.page_content) for page in pages[:30])
|
47 |
+
|
48 |
+
stuff_chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
49 |
+
|
50 |
+
stuff_answer = stuff_chain({"input_documents": pages, "question": question, "context": context}, return_only_outputs=True)
|
51 |
+
|
52 |
+
return stuff_answer['output_text']
|
53 |
+
|
54 |
+
# Membuat antarmuka pengguna
|
55 |
+
with gr.Blocks() as demo:
|
56 |
+
gr.Markdown('# RAG Q&A Bot with Gemini - Pro')
|
57 |
+
gr.Markdown('### Hands-On LLM')
|
58 |
+
|
59 |
+
link_input = gr.Textbox(label="Input Link Paper atau PDF", placeholder="Paste PDF disini")
|
60 |
+
question_input = gr.Textbox(label="Tanyakan Dokumen", placeholder="Tanyakan Dokumen:")
|
61 |
+
chatbot = gr.Textbox(label="Answer - GeminiPro")
|
62 |
+
|
63 |
+
ask_button = gr.Button("Ask Question")
|
64 |
+
ask_button.click(initialize, inputs=[link_input, question_input], outputs=[chatbot])
|
65 |
+
|
66 |
+
# Meluncurkan antarmuka pengguna
|
67 |
+
demo.queue().launch(debug=True)
|
downloaded_paper.pdf
ADDED
Binary file (924 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
urllib3
|
2 |
+
gradio
|
3 |
+
pandas
|
4 |
+
langchain
|
5 |
+
langchain_google_genai==0.0.4
|
6 |
+
pypdf
|