Commit
·
7df44cb
1
Parent(s):
17c61e4
first commit
Browse files- .gitignore +152 -0
- app.py +55 -0
- requirements.txt +7 -0
.gitignore
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# File PDF
|
7 |
+
*.pdf
|
8 |
+
|
9 |
+
# C extensions
|
10 |
+
*.so
|
11 |
+
|
12 |
+
# Local folder
|
13 |
+
local_folder
|
14 |
+
project_demo
|
15 |
+
project_demo/
|
16 |
+
logs/
|
17 |
+
local_folder/
|
18 |
+
/demo.py
|
19 |
+
demo.py
|
20 |
+
runs/
|
21 |
+
downloaded_paper.pdf/
|
22 |
+
|
23 |
+
# Large folders
|
24 |
+
weights/
|
25 |
+
videos/
|
26 |
+
images/
|
27 |
+
|
28 |
+
|
29 |
+
# Distribution / packaging
|
30 |
+
.Python
|
31 |
+
build/
|
32 |
+
develop-eggs/
|
33 |
+
dist/
|
34 |
+
downloads/
|
35 |
+
eggs/
|
36 |
+
.eggs/
|
37 |
+
lib/
|
38 |
+
lib64/
|
39 |
+
parts/
|
40 |
+
sdist/
|
41 |
+
var/
|
42 |
+
wheels/
|
43 |
+
pip-wheel-metadata/
|
44 |
+
share/python-wheels/
|
45 |
+
*.egg-info/
|
46 |
+
.installed.cfg
|
47 |
+
*.egg
|
48 |
+
MANIFEST
|
49 |
+
|
50 |
+
# PyInstaller
|
51 |
+
# Usually these files are written by a python script from a template
|
52 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
53 |
+
*.manifest
|
54 |
+
*.spec
|
55 |
+
|
56 |
+
.vscode
|
57 |
+
|
58 |
+
# Installer logs
|
59 |
+
pip-log.txt
|
60 |
+
pip-delete-this-directory.txt
|
61 |
+
|
62 |
+
# Unit test / coverage reports
|
63 |
+
htmlcov/
|
64 |
+
.tox/
|
65 |
+
.nox/
|
66 |
+
.coverage
|
67 |
+
.coverage.*
|
68 |
+
.cache
|
69 |
+
nosetests.xml
|
70 |
+
coverage.xml
|
71 |
+
*.cover
|
72 |
+
*.py,cover
|
73 |
+
.hypothesis/
|
74 |
+
.pytest_cache/
|
75 |
+
|
76 |
+
# Translations
|
77 |
+
*.mo
|
78 |
+
*.pot
|
79 |
+
|
80 |
+
# Django stuff:
|
81 |
+
*.log
|
82 |
+
local_settings.py
|
83 |
+
db.sqlite3
|
84 |
+
db.sqlite3-journal
|
85 |
+
|
86 |
+
# Flask stuff:
|
87 |
+
instance/
|
88 |
+
.webassets-cache
|
89 |
+
|
90 |
+
# Scrapy stuff:
|
91 |
+
.scrapy
|
92 |
+
|
93 |
+
# Sphinx documentation
|
94 |
+
docs/_build/
|
95 |
+
|
96 |
+
# PyBuilder
|
97 |
+
target/
|
98 |
+
|
99 |
+
# Jupyter Notebook
|
100 |
+
.ipynb_checkpoints
|
101 |
+
|
102 |
+
# IPython
|
103 |
+
profile_default/
|
104 |
+
ipython_config.py
|
105 |
+
|
106 |
+
# pyenv
|
107 |
+
.python-version
|
108 |
+
|
109 |
+
# pipenv
|
110 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
111 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
112 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
113 |
+
# install all needed dependencies.
|
114 |
+
#Pipfile.lock
|
115 |
+
|
116 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
117 |
+
__pypackages__/
|
118 |
+
|
119 |
+
# Celery stuff
|
120 |
+
celerybeat-schedule
|
121 |
+
celerybeat.pid
|
122 |
+
|
123 |
+
# SageMath parsed files
|
124 |
+
*.sage.py
|
125 |
+
|
126 |
+
# Environments
|
127 |
+
.env
|
128 |
+
.venv
|
129 |
+
env/
|
130 |
+
venv/
|
131 |
+
venv_/
|
132 |
+
ENV/
|
133 |
+
env.bak/
|
134 |
+
venv.bak/
|
135 |
+
|
136 |
+
# Spyder project settings
|
137 |
+
.spyderproject
|
138 |
+
.spyproject
|
139 |
+
|
140 |
+
# Rope project settings
|
141 |
+
.ropeproject
|
142 |
+
|
143 |
+
# mkdocs documentation
|
144 |
+
/site
|
145 |
+
|
146 |
+
# mypy
|
147 |
+
.mypy_cache/
|
148 |
+
.dmypy.json
|
149 |
+
dmypy.json
|
150 |
+
|
151 |
+
# Pyre type checker
|
152 |
+
.pyre/
|
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from langchain import PromptTemplate
|
4 |
+
from langchain.chains.question_answering import load_qa_chain
|
5 |
+
from langchain.document_loaders import PyPDFLoader
|
6 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
7 |
+
import google.generativeai as genai
|
8 |
+
|
9 |
+
# Load environment variables from .env file
|
10 |
+
from dotenv import load_dotenv
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
# Fungsi untuk inisialisasi
|
14 |
+
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 = stuff_chain({"input_documents": pages, "question": question, "context": context}, return_only_outputs=True)
|
31 |
+
return stuff_answer['output_text']
|
32 |
+
else:
|
33 |
+
return None
|
34 |
+
|
35 |
+
# Membuat antarmuka pengguna dengan Streamlit
|
36 |
+
st.title('RAG Q&A Bot with Gemini - Pro')
|
37 |
+
|
38 |
+
# Layout untuk input dan tombol di sebelah kiri
|
39 |
+
st.sidebar.title("Input")
|
40 |
+
file_upload = st.sidebar.file_uploader("Upload PDF", type=["pdf"])
|
41 |
+
question_input = st.sidebar.text_input("Tanyakan Dokumen", "Tanyakan Dokumen:")
|
42 |
+
ask_button = st.sidebar.button("Ask Question")
|
43 |
+
|
44 |
+
# Jika tombol "Ask Question" ditekan
|
45 |
+
if ask_button:
|
46 |
+
if file_upload is not None:
|
47 |
+
with open("uploaded_pdf.pdf", "wb") as f:
|
48 |
+
f.write(file_upload.getbuffer())
|
49 |
+
chatbot_answer = initialize("uploaded_pdf.pdf", question_input)
|
50 |
+
if chatbot_answer:
|
51 |
+
st.text_area("Answer - GeminiPro", value=chatbot_answer, height=200)
|
52 |
+
else:
|
53 |
+
st.error("Terjadi kesalahan saat memproses dokumen. Pastikan file PDF valid.")
|
54 |
+
else:
|
55 |
+
st.error("Mohon unggah file PDF untuk melanjutkan.")
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
urllib3
|
2 |
+
gradio
|
3 |
+
pandas
|
4 |
+
langchain
|
5 |
+
langchain_google_genai==0.0.4
|
6 |
+
pypdf
|
7 |
+
python-dotenv
|