Spaces:
Runtime error
Runtime error
karuniaperjuangan
commited on
Commit
•
8f9ef57
1
Parent(s):
347a591
Upload 8 files
Browse files- .gitignore +162 -0
- LICENSE +21 -0
- README.md +1 -13
- app.py +62 -0
- docs.index +0 -0
- extract_pdf.py +49 -0
- faiss_store.pkl +3 -0
- requirements.txt +7 -0
.gitignore
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Because notebook is only for testing
|
10 |
+
|
11 |
+
# Distribution / packaging
|
12 |
+
.Python
|
13 |
+
build/
|
14 |
+
develop-eggs/
|
15 |
+
dist/
|
16 |
+
downloads/
|
17 |
+
eggs/
|
18 |
+
.eggs/
|
19 |
+
lib/
|
20 |
+
lib64/
|
21 |
+
parts/
|
22 |
+
sdist/
|
23 |
+
var/
|
24 |
+
wheels/
|
25 |
+
share/python-wheels/
|
26 |
+
*.egg-info/
|
27 |
+
.installed.cfg
|
28 |
+
*.egg
|
29 |
+
MANIFEST
|
30 |
+
|
31 |
+
# PyInstaller
|
32 |
+
# Usually these files are written by a python script from a template
|
33 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
34 |
+
*.manifest
|
35 |
+
*.spec
|
36 |
+
|
37 |
+
# Installer logs
|
38 |
+
pip-log.txt
|
39 |
+
pip-delete-this-directory.txt
|
40 |
+
|
41 |
+
# Unit test / coverage reports
|
42 |
+
htmlcov/
|
43 |
+
.tox/
|
44 |
+
.nox/
|
45 |
+
.coverage
|
46 |
+
.coverage.*
|
47 |
+
.cache
|
48 |
+
nosetests.xml
|
49 |
+
coverage.xml
|
50 |
+
*.cover
|
51 |
+
*.py,cover
|
52 |
+
.hypothesis/
|
53 |
+
.pytest_cache/
|
54 |
+
cover/
|
55 |
+
|
56 |
+
# Translations
|
57 |
+
*.mo
|
58 |
+
*.pot
|
59 |
+
|
60 |
+
# Django stuff:
|
61 |
+
*.log
|
62 |
+
local_settings.py
|
63 |
+
db.sqlite3
|
64 |
+
db.sqlite3-journal
|
65 |
+
|
66 |
+
# Flask stuff:
|
67 |
+
instance/
|
68 |
+
.webassets-cache
|
69 |
+
|
70 |
+
# Scrapy stuff:
|
71 |
+
.scrapy
|
72 |
+
|
73 |
+
# Sphinx documentation
|
74 |
+
docs/_build/
|
75 |
+
|
76 |
+
# PyBuilder
|
77 |
+
.pybuilder/
|
78 |
+
target/
|
79 |
+
|
80 |
+
# Jupyter Notebook
|
81 |
+
.ipynb_checkpoints
|
82 |
+
|
83 |
+
# IPython
|
84 |
+
profile_default/
|
85 |
+
ipython_config.py
|
86 |
+
|
87 |
+
# pyenv
|
88 |
+
# For a library or package, you might want to ignore these files since the code is
|
89 |
+
# intended to run in multiple environments; otherwise, check them in:
|
90 |
+
# .python-version
|
91 |
+
|
92 |
+
# pipenv
|
93 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
94 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
95 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
96 |
+
# install all needed dependencies.
|
97 |
+
#Pipfile.lock
|
98 |
+
|
99 |
+
# poetry
|
100 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
101 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
102 |
+
# commonly ignored for libraries.
|
103 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
104 |
+
#poetry.lock
|
105 |
+
|
106 |
+
# pdm
|
107 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
108 |
+
#pdm.lock
|
109 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
110 |
+
# in version control.
|
111 |
+
# https://pdm.fming.dev/#use-with-ide
|
112 |
+
.pdm.toml
|
113 |
+
|
114 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
115 |
+
__pypackages__/
|
116 |
+
|
117 |
+
# Celery stuff
|
118 |
+
celerybeat-schedule
|
119 |
+
celerybeat.pid
|
120 |
+
|
121 |
+
# SageMath parsed files
|
122 |
+
*.sage.py
|
123 |
+
|
124 |
+
# Environments
|
125 |
+
.env
|
126 |
+
.venv
|
127 |
+
env/
|
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/
|
150 |
+
|
151 |
+
# pytype static type analyzer
|
152 |
+
.pytype/
|
153 |
+
|
154 |
+
# Cython debug symbols
|
155 |
+
cython_debug/
|
156 |
+
|
157 |
+
# PyCharm
|
158 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
159 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
160 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
161 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
162 |
+
#.idea/
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 karuniaperjuangan
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,13 +1 @@
|
|
1 |
-
|
2 |
-
title: Bangkit Chatbot
|
3 |
-
emoji: 🏢
|
4 |
-
colorFrom: gray
|
5 |
-
colorTo: purple
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 3.42.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: cc-by-3.0
|
11 |
-
---
|
12 |
-
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
# Chatbot Konseling Masalah Kekerasan Seksual
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Ini adalah kode utama untuk menjalankan aplikasi chatbot
|
3 |
+
"""
|
4 |
+
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
load_dotenv() # take environment variables from .env.
|
8 |
+
|
9 |
+
import os
|
10 |
+
|
11 |
+
from langchain.chat_models import ChatOpenAI
|
12 |
+
from langchain.prompts.chat import (
|
13 |
+
ChatPromptTemplate,
|
14 |
+
SystemMessagePromptTemplate,
|
15 |
+
AIMessagePromptTemplate,
|
16 |
+
HumanMessagePromptTemplate,
|
17 |
+
)
|
18 |
+
import faiss
|
19 |
+
from langchain.chat_models import ChatOpenAI
|
20 |
+
from langchain.chains import RetrievalQAWithSourcesChain
|
21 |
+
import pickle
|
22 |
+
from langchain.vectorstores import FAISS
|
23 |
+
from typing import List
|
24 |
+
import gradio as gr
|
25 |
+
from langchain.schema import AIMessage, HumanMessage, SystemMessage
|
26 |
+
|
27 |
+
chat = ChatOpenAI(temperature=0)
|
28 |
+
|
29 |
+
# Load the LangChain.
|
30 |
+
index = faiss.read_index("docs.index")
|
31 |
+
|
32 |
+
with open("faiss_store.pkl", "rb") as f:
|
33 |
+
store:FAISS = pickle.load(f)
|
34 |
+
|
35 |
+
store.index = index
|
36 |
+
|
37 |
+
def ask_retrieval_qa(question:str,history:List[List[str]]):
|
38 |
+
answers = store.similarity_search("Apa kontak yang bisa dihubungi untuk menghadapi kasus kekerasan seksual?", k=1)
|
39 |
+
messages = [
|
40 |
+
SystemMessage(content=f"""Kamu adalah sebuah chatbot yang bernama Bangkit. Tugasmu adalah membantu orang-orang yang mengalami masalah kekerasan seksual. Kamu akan memberikan informasi yang dapat membantu seseorang untuk melakukan tindak lanjut terhadap kasus kekerasan seksual yang dialami.
|
41 |
+
Anda bisa menggunakan potongan UU No 12 tahun 2022 berikut untuk membantu Anda:
|
42 |
+
{answers[0].page_content}
|
43 |
+
Sumber: {answers[0].metadata}"""),
|
44 |
+
|
45 |
+
]
|
46 |
+
for human,ai in history:
|
47 |
+
messages.append(HumanMessage(content=human))
|
48 |
+
messages.append(AIMessage(content=ai))
|
49 |
+
messages.append(HumanMessage(content=question))
|
50 |
+
response = chat(messages)
|
51 |
+
return response.content
|
52 |
+
|
53 |
+
with gr.Blocks() as demo:
|
54 |
+
|
55 |
+
gr.Markdown("""<h1 style="text-align:center">Aplikasi Bangkit (Intelligent Chatbot for Sexual Violence Behaviour) </h1>""")
|
56 |
+
gr.Markdown(
|
57 |
+
"""
|
58 |
+
Gunakan chatbot di bawah untuk menangani kasus kekerasan seksual yang anda alami.
|
59 |
+
""" )
|
60 |
+
gr.ChatInterface(ask_retrieval_qa)
|
61 |
+
demo.launch(auth=(os.environ.get("GRADIO_USERNAME"),os.environ.get("GRADIO_PASSWORD")))
|
62 |
+
|
docs.index
ADDED
Binary file (172 kB). View file
|
|
extract_pdf.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Kode di bawah digunakan untuk mengextract teks dari file pdf yang ada di folder document
|
3 |
+
"""
|
4 |
+
from pathlib import Path
|
5 |
+
from langchain.text_splitter import CharacterTextSplitter
|
6 |
+
import faiss
|
7 |
+
from langchain.vectorstores import FAISS
|
8 |
+
from langchain.embeddings import OpenAIEmbeddings
|
9 |
+
import pickle
|
10 |
+
import re
|
11 |
+
|
12 |
+
from langchain.document_loaders import PyPDFLoader
|
13 |
+
|
14 |
+
paths = Path("document/").glob("**/*.pdf")
|
15 |
+
docs = []
|
16 |
+
for path in paths:
|
17 |
+
path = str(path)
|
18 |
+
loader = PyPDFLoader(path)
|
19 |
+
pages = loader.load_and_split()
|
20 |
+
pages
|
21 |
+
document = []
|
22 |
+
for page in pages:
|
23 |
+
content = page.page_content
|
24 |
+
#Kode di bawah untuk menghilangkan header dan footer
|
25 |
+
content = re.sub(r"PRES(.*)\n(.*)\n-.*-\n", " ", content)
|
26 |
+
content = re.sub(r"FRES(.*)\n(.*)\n-.*-\n", " ", content)
|
27 |
+
content = re.sub(r"PRESIDEN\nREPUBLIK INDONESIA\n.11-\n", " ", content)
|
28 |
+
content = re.sub(r"PRESIDEN\nREPUBLIK INOONESIA\n_55_\n", " ", content)
|
29 |
+
content = re.sub(r"PRESIDEN\nREPUBLIK INDONESIA\n.20 -\n", " ", content)
|
30 |
+
content = re.sub(r"PRESIDEN\nREPUBLIK INDONESIA\n24-", " ", content)
|
31 |
+
content = re.sub(r"PRESIOEN\nREPUBLIK INDONESIA\n-39\n", " ", content)
|
32 |
+
content = re.sub(r"PRESIDEN\nREPUELIK INDONESIA\nL2-\n", " ", content)
|
33 |
+
document.append({"content": content,"metadata":{'source':page.metadata['source']}})
|
34 |
+
document
|
35 |
+
|
36 |
+
#join content every 3 pages
|
37 |
+
|
38 |
+
for i in range(0, len(pages), 3):
|
39 |
+
docs.append({'content':" ".join([page['content'] for page in document][i:i+3]),
|
40 |
+
'metadata':{'source':document[i]['metadata']['source']}
|
41 |
+
})
|
42 |
+
|
43 |
+
# Here we create a vector store from the documents and save it to disk.
|
44 |
+
list_content = [doc['content'] for doc in docs]
|
45 |
+
list_metadata = [doc['metadata'] for doc in docs]
|
46 |
+
store = FAISS.from_texts(list_content, OpenAIEmbeddings(), metadatas=list_metadata)
|
47 |
+
faiss.write_index(store.index, "docs.index")
|
48 |
+
store.index = None
|
49 |
+
|
faiss_store.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e1bad3bcd607c13b2d45bb5c618519000a4367004be45ac237eb700fbf77cb9a
|
3 |
+
size 90972
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
openai
|
3 |
+
faiss-cpu
|
4 |
+
gradio
|
5 |
+
tiktoken
|
6 |
+
python-dotenv
|
7 |
+
pypdf
|