Spaces:
Running
on
Zero
Running
on
Zero
ghengx
commited on
Commit
•
7c2445a
1
Parent(s):
df2e419
update
Browse files- .gitignore +5 -0
- app.py +80 -18
- requirements.txt +2 -0
.gitignore
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
venv
|
3 |
+
test.py
|
4 |
+
__pycache__
|
5 |
+
backend_fn
|
app.py
CHANGED
@@ -1,7 +1,27 @@
|
|
1 |
import spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
4 |
from threading import Thread
|
|
|
|
|
5 |
|
6 |
"""
|
7 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
@@ -17,14 +37,19 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
17 |
|
18 |
streamer = TextIteratorStreamer(tokenizer, timeout=300, skip_prompt=True, skip_special_tokens=True)
|
19 |
|
|
|
|
|
|
|
|
|
|
|
20 |
@spaces.GPU
|
21 |
def respond(
|
22 |
message,
|
23 |
history: list[tuple[str, str]],
|
24 |
# system_message,
|
25 |
-
max_tokens,
|
26 |
-
temperature,
|
27 |
-
top_p,
|
28 |
):
|
29 |
messages = [
|
30 |
{"role": "system", "content": "You are a professional lawyer who is familiar with Malaysia Law."}
|
@@ -64,24 +89,61 @@ def respond(
|
|
64 |
"""
|
65 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
66 |
"""
|
67 |
-
demo = gr.ChatInterface(
|
68 |
-
respond,
|
69 |
-
additional_inputs=[
|
70 |
-
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
71 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
72 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.1, step=0.1, label="Temperature"),
|
73 |
-
gr.Slider(
|
74 |
-
minimum=0.1,
|
75 |
-
maximum=1.0,
|
76 |
-
value=0.95,
|
77 |
-
step=0.05,
|
78 |
-
label="Top-p (nucleus sampling)",
|
79 |
-
),
|
80 |
-
],
|
81 |
-
)
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
if __name__ == "__main__":
|
85 |
demo.launch(
|
86 |
|
87 |
)
|
|
|
|
1 |
import spaces
|
2 |
+
import os
|
3 |
+
|
4 |
+
from huggingface_hub import Repository
|
5 |
+
from huggingface_hub import login
|
6 |
+
|
7 |
+
login(token = os.environ['HUB_TOKEN'])
|
8 |
+
|
9 |
+
repo = Repository(
|
10 |
+
local_dir="backend_fn",
|
11 |
+
repo_type="dataset",
|
12 |
+
clone_from=os.environ['DATASET'],
|
13 |
+
token=True,
|
14 |
+
git_email='[email protected]'
|
15 |
+
)
|
16 |
+
repo.git_pull()
|
17 |
+
|
18 |
+
import json
|
19 |
+
import uuid
|
20 |
import gradio as gr
|
21 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
22 |
from threading import Thread
|
23 |
+
from backend_fn.feedback import feedback
|
24 |
+
from gradio_modal import Modal
|
25 |
|
26 |
"""
|
27 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
|
|
37 |
|
38 |
streamer = TextIteratorStreamer(tokenizer, timeout=300, skip_prompt=True, skip_special_tokens=True)
|
39 |
|
40 |
+
histories = []
|
41 |
+
action = None
|
42 |
+
|
43 |
+
session_id = uuid.uuid1().__str__()
|
44 |
+
|
45 |
@spaces.GPU
|
46 |
def respond(
|
47 |
message,
|
48 |
history: list[tuple[str, str]],
|
49 |
# system_message,
|
50 |
+
max_tokens = 4096,
|
51 |
+
temperature = 0.01,
|
52 |
+
top_p = 0.95,
|
53 |
):
|
54 |
messages = [
|
55 |
{"role": "system", "content": "You are a professional lawyer who is familiar with Malaysia Law."}
|
|
|
89 |
"""
|
90 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
91 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
+
def submit_feedback(value):
|
94 |
+
feedback(session_id, json.dumps(histories), value, action)
|
95 |
+
|
96 |
+
|
97 |
+
with gr.Blocks() as demo:
|
98 |
+
def vote(history,data: gr.LikeData):
|
99 |
+
global histories
|
100 |
+
global action
|
101 |
+
histories = history
|
102 |
+
action = data.liked
|
103 |
+
|
104 |
+
with Modal(visible=False) as modal:
|
105 |
+
textb = gr.Textbox(
|
106 |
+
label='Actual response',
|
107 |
+
info='Leave blank if the answer is good enough'
|
108 |
+
)
|
109 |
+
|
110 |
+
submit_btn = gr.Button(
|
111 |
+
'Submit'
|
112 |
+
)
|
113 |
+
|
114 |
+
submit_btn.click(submit_feedback,textb)
|
115 |
+
submit_btn.click(lambda: Modal(visible=False), None, modal)
|
116 |
+
submit_btn.click(lambda x: gr.update(value=''), [],[textb])
|
117 |
+
|
118 |
+
|
119 |
+
ci = gr.ChatInterface(
|
120 |
+
respond,
|
121 |
+
# fill_height=True
|
122 |
+
# additional_inputs=[
|
123 |
+
# # gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
124 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
125 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.1, step=0.1, label="Temperature"),
|
126 |
+
# gr.Slider(
|
127 |
+
# minimum=0.1,
|
128 |
+
# maximum=1.0,
|
129 |
+
# value=0.95,
|
130 |
+
# step=0.05,
|
131 |
+
# label="Top-p (nucleus sampling)",
|
132 |
+
# ),
|
133 |
+
# ],
|
134 |
+
)
|
135 |
+
|
136 |
+
|
137 |
+
ci.chatbot.show_copy_button=True
|
138 |
+
# ci.chatbot.value=[(None,"Hello! I'm here to assist you with understanding the laws and acts of Malaysia.")]
|
139 |
+
# ci.chatbot.height=500
|
140 |
+
|
141 |
+
ci.chatbot.like(vote, ci.chatbot, None).then(
|
142 |
+
lambda: Modal(visible=True), None, modal
|
143 |
+
)
|
144 |
|
145 |
if __name__ == "__main__":
|
146 |
demo.launch(
|
147 |
|
148 |
)
|
149 |
+
|
requirements.txt
CHANGED
@@ -11,6 +11,7 @@ filelock==3.16.1
|
|
11 |
fsspec==2024.10.0
|
12 |
gradio==5.4.0
|
13 |
gradio_client==1.4.2
|
|
|
14 |
h11==0.14.0
|
15 |
httpcore==1.0.6
|
16 |
httpx==0.27.2
|
@@ -32,6 +33,7 @@ pydantic==2.9.2
|
|
32 |
pydantic_core==2.23.4
|
33 |
pydub==0.25.1
|
34 |
Pygments==2.18.0
|
|
|
35 |
python-dateutil==2.9.0.post0
|
36 |
python-multipart==0.0.12
|
37 |
pytz==2024.2
|
|
|
11 |
fsspec==2024.10.0
|
12 |
gradio==5.4.0
|
13 |
gradio_client==1.4.2
|
14 |
+
gradio_modal==0.0.4
|
15 |
h11==0.14.0
|
16 |
httpcore==1.0.6
|
17 |
httpx==0.27.2
|
|
|
33 |
pydantic_core==2.23.4
|
34 |
pydub==0.25.1
|
35 |
Pygments==2.18.0
|
36 |
+
PyMySQL==1.1.1
|
37 |
python-dateutil==2.9.0.post0
|
38 |
python-multipart==0.0.12
|
39 |
pytz==2024.2
|