khangey commited on
Commit
1fa184f
·
1 Parent(s): 9ae475e

Upload 7 files

Browse files
Files changed (7) hide show
  1. README.md +11 -7
  2. chat.py +66 -0
  3. gitattributes.txt +34 -0
  4. gitignore.txt +163 -0
  5. requirements.txt +4 -0
  6. static/app.css +17 -0
  7. store.py +42 -0
README.md CHANGED
@@ -1,13 +1,17 @@
1
  ---
2
- title: KhangeyAI
3
- emoji: 👁
4
- colorFrom: blue
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 4.1.1
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
1
  ---
2
+ title: Chatbot Tibetan
3
+ emoji: 🤖
4
+ colorFrom: green
5
+ colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 3.26.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
+ <iframe
13
+ src="https://openpecha-chatbot-tibetan.hf.space"
14
+ frameborder="0"
15
+ width="850"
16
+ height="450"
17
+ ></iframe>
chat.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Tuple
3
+
4
+ import openai
5
+
6
+
7
+ class ChatGpt:
8
+ def __init__(self, api_key, max_tokens=4096):
9
+ self.api_key = api_key
10
+ self.max_tokens = max_tokens
11
+ self.message_history = []
12
+ self.total_tokens = 0
13
+
14
+ # Set up the OpenAI API client
15
+ openai.api_key = self.api_key
16
+
17
+ def clear_history(self):
18
+ self.message_history = []
19
+ self.total_tokens = 0
20
+
21
+ def add_message(self, role: str, content: str):
22
+ self.message_history.append({"role": role, "content": content})
23
+ self._truncate_history()
24
+
25
+ def add_system_message(self, content: str):
26
+ self.add_message("system", content)
27
+
28
+ def generate_response(self, user_input: str) -> str:
29
+ self.add_message("user", user_input)
30
+ response = self._call_openai_api(self.message_history)
31
+ self.add_message("assistant", response)
32
+
33
+ return response
34
+
35
+ def _truncate_history(self):
36
+ while self.total_tokens > self.max_tokens:
37
+ if self.message_history[0]["role"] != "system":
38
+ self.message_history.pop(0)
39
+ else:
40
+ break
41
+
42
+ def _call_openai_api(self, messages) -> str:
43
+ response = openai.ChatCompletion.create(
44
+ model="gpt-3.5-turbo", messages=messages
45
+ )
46
+ self.total_tokens += response["usage"]["total_tokens"]
47
+ return response["choices"][0]["message"]["content"].strip()
48
+
49
+
50
+ if __name__ == "__main__":
51
+ chat = ChatGpt(os.getenv("OPENAI_API_KEY"))
52
+
53
+ chat.add_system_message("The assistant can answer questions and tell jokes.")
54
+ user_input = "Tell me a joke."
55
+ user_msg, bot_response = chat.generate_response(user_input)
56
+ assert user_msg == user_input
57
+ print("User:", user_msg)
58
+ print("Assistant:", bot_response)
59
+ print("Total Tokens:", chat.total_tokens)
60
+
61
+ user_input = "another one"
62
+ user_msg, bot_response = chat.generate_response(user_input)
63
+ assert user_msg == user_input
64
+ print("User:", user_msg)
65
+ print("Assistant:", bot_response)
66
+ print("Total Tokens:", chat.total_tokens)
gitattributes.txt ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
gitignore.txt ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ env/
126
+ venv/
127
+ ENV/
128
+ env.bak/
129
+ venv.bak/
130
+
131
+ # Spyder project settings
132
+ .spyderproject
133
+ .spyproject
134
+
135
+ # Rope project settings
136
+ .ropeproject
137
+
138
+ # mkdocs documentation
139
+ /site
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
159
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
+ #.idea/
161
+
162
+ .vscode
163
+ prompts.py
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==3.26.0
2
+ requests==2.28.2
3
+ boto3==1.26.113
4
+ openai==0.27.4, <1.0
static/app.css ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #maiChatHistory .message-wrap div {
2
+ font-size: 30px;
3
+ }
4
+
5
+ #maiChatInput textarea {
6
+ font-size: 30px
7
+ }
8
+
9
+ @media screen and (min-width: 768px) {
10
+ #maiChatInput textarea {
11
+ font-size: 20px
12
+ }
13
+
14
+ #maiChatHistory .message-wrap div {
15
+ font-size: 20px;
16
+ }
17
+ }
store.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import uuid
3
+ from datetime import datetime
4
+ from typing import Dict, Tuple
5
+
6
+ import boto3
7
+
8
+ dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
9
+ all_chats_table = dynamodb.Table("ChatbotTibetanAllChats")
10
+
11
+
12
+ def store_message_pair(chat_id: str, msg_pair: Dict[str, Tuple[str, str]]):
13
+ """Store the chat history to DynamoDB
14
+
15
+ Args:
16
+ chat_id: The ID of the chat
17
+ msg_pair: tuple with 2 items (user_message, bot_response)
18
+ lang: The language of the msg_pair
19
+ order: The order of the msg_pair in chat history
20
+ """
21
+
22
+ # Add the new message to the chat history
23
+ msg_pair_id = uuid.uuid4().hex[:10]
24
+ response = all_chats_table.put_item(
25
+ Item={
26
+ "msg_pair_id": msg_pair_id,
27
+ "msg_pair": json.dumps(msg_pair, ensure_ascii=False),
28
+ "created_at": datetime.now().isoformat(),
29
+ "chat_id": chat_id,
30
+ }
31
+ )
32
+ return response
33
+
34
+
35
+ if __name__ == "__main__":
36
+ # Replace with your own DynamoDB table name and chat ID
37
+ chat_id = str(uuid.uuid4())
38
+
39
+ # Replace with your own chat history (list of tuples or list of dictionaries)
40
+ msg_pair = {"bo": ("hello", "hello"), "en": ("hello", "hello")}
41
+ response = store_message_pair(chat_id, msg_pair)
42
+ print(response)