Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,128 +1,28 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import spaces
|
4 |
-
import gradio as gr
|
5 |
|
6 |
-
|
7 |
-
from huggingface_hub import login
|
8 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
9 |
|
10 |
-
login(os.environ.get("HF_TOKEN"))
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
message: str,
|
23 |
-
chat_history: list[tuple[str, str]],
|
24 |
-
system_prompt: str,
|
25 |
-
max_new_tokens: int,
|
26 |
-
temperature: float,
|
27 |
-
top_p: float,
|
28 |
-
top_k: int,
|
29 |
-
repetition_penalty: int
|
30 |
-
):
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
conversation.append({"role": "system", "content": system_prompt})
|
35 |
|
36 |
-
for user, assistant in chat_history:
|
37 |
-
conversation.append({"role": "user", "content": user})
|
38 |
-
conversation.append({"role": "assistant", "content": assistant})
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
conversation,
|
45 |
-
add_generation_prompt=True,
|
46 |
-
return_tensors="pt",
|
47 |
-
return_dict=True
|
48 |
-
).to(model.device).values()
|
49 |
-
|
50 |
-
terminators = [
|
51 |
-
tokenizer.eos_token_id,
|
52 |
-
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
53 |
-
]
|
54 |
-
|
55 |
-
generate_kwargs = dict(
|
56 |
-
{"input_ids": input_ids, "attention_mask": attention_mask},
|
57 |
-
streamer=streamer,
|
58 |
-
do_sample=True,
|
59 |
-
temperature=temperature,
|
60 |
-
max_new_tokens=max_new_tokens,
|
61 |
-
eos_token_id=terminators,
|
62 |
-
top_k=top_k,
|
63 |
-
repetition_penalty=repetition_penalty,
|
64 |
-
top_p=top_p
|
65 |
-
)
|
66 |
-
|
67 |
-
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
68 |
-
t.start()
|
69 |
-
outputs = []
|
70 |
-
for new_token in streamer:
|
71 |
-
outputs.append(new_token)
|
72 |
-
yield "".join(outputs)
|
73 |
-
|
74 |
-
|
75 |
-
gr.ChatInterface(
|
76 |
-
fn=generate,
|
77 |
-
title="🦙 Llama-3 8B Chat",
|
78 |
-
description="",
|
79 |
-
additional_inputs=[
|
80 |
-
gr.Textbox(
|
81 |
-
label="System prompt",
|
82 |
-
lines=5,
|
83 |
-
value="Anda adalah asisten cerdas yang mahir berbahasa Indonesia. Anda dapat memahami dan merespons pertanyaan dalam berbagai bahasa, tetapi selalu menggunakan bahasa Indonesia yang baik dan benar dalam merespons. Anda ramah, sopan, dan berusaha memberikan jawaban yang jelas dan bermanfaat bagi pengguna. Jangan merespon dengan bahasa selain bahasa Indonesia!"
|
84 |
-
),
|
85 |
-
gr.Slider(
|
86 |
-
label="Max new tokens",
|
87 |
-
minimum=1,
|
88 |
-
maximum=2048,
|
89 |
-
step=1,
|
90 |
-
value=1024,
|
91 |
-
),
|
92 |
-
gr.Slider(
|
93 |
-
label="Temperature",
|
94 |
-
minimum=0.1,
|
95 |
-
maximum=4.0,
|
96 |
-
step=0.1,
|
97 |
-
value=0.6,
|
98 |
-
),
|
99 |
-
gr.Slider(
|
100 |
-
label="Top-p (nucleus sampling)",
|
101 |
-
minimum=0.05,
|
102 |
-
maximum=1.0,
|
103 |
-
step=0.05,
|
104 |
-
value=0.9,
|
105 |
-
),
|
106 |
-
gr.Slider(
|
107 |
-
label="Top-k",
|
108 |
-
minimum=1,
|
109 |
-
maximum=1000,
|
110 |
-
step=1,
|
111 |
-
value=50,
|
112 |
-
),
|
113 |
-
gr.Slider(
|
114 |
-
label="Repetition penalty",
|
115 |
-
minimum=1.0,
|
116 |
-
maximum=2.0,
|
117 |
-
step=0.05,
|
118 |
-
value=1.2,
|
119 |
-
),
|
120 |
-
],
|
121 |
-
stop_btn=None,
|
122 |
-
examples=[
|
123 |
-
["Halo apa kabar?"],
|
124 |
-
["Apa manfaat berolahraga secara teratur?"],
|
125 |
-
["Jika Budi berjalan sejauh 5 meter, berapa jumlah anak ayam bapaknya Budi?"],
|
126 |
-
["Siapa presiden pertama Indonesia?"]
|
127 |
-
],
|
128 |
-
).queue().launch()
|
|
|
1 |
+
# Use a pipeline as a high-level helper
|
2 |
+
from transformers import pipeline
|
|
|
|
|
3 |
|
4 |
+
pipe = pipeline("text-generation", model="tiiuae/falcon-40b-instruct")
|
|
|
|
|
5 |
|
|
|
6 |
|
7 |
+
# Function to interact with the chatbot
|
8 |
+
def chat_with_bot():
|
9 |
+
while True:
|
10 |
+
# Take user input
|
11 |
+
question = input("You: ")
|
12 |
|
13 |
+
# Exit if user inputs 'exit'
|
14 |
+
if question.lower() == "exit":
|
15 |
+
print("Chatbot: Goodbye!")
|
16 |
+
break
|
|
|
17 |
|
18 |
+
# Generate answer using the model
|
19 |
+
answer = qa_pipeline(question=question, context="")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# Print the answer
|
22 |
+
print("Chatbot:", answer['answer'])
|
|
|
23 |
|
|
|
|
|
|
|
24 |
|
25 |
+
# Start chatting
|
26 |
+
if __name__ == "__main__":
|
27 |
+
print("Welcome to the chatbot! Type 'exit' to end the conversation.")
|
28 |
+
chat_with_bot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|