Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,178 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
.
|
31 |
-
|
32 |
-
|
33 |
-
pre, code {
|
34 |
-
direction: ltr !important;
|
35 |
-
unicode-bidi: plaintext !important;
|
36 |
-
}
|
37 |
-
"""
|
38 |
-
|
39 |
-
system_prompt = "You are a helpful assistant."
|
40 |
-
|
41 |
-
# Load the model
|
42 |
-
model_id = "hosseinhimself/ISANG-1.0-8B"
|
43 |
-
model = AutoModelForCausalLM.from_pretrained(
|
44 |
-
model_id, device_map="auto", torch_dtype=torch.bfloat16
|
45 |
-
)
|
46 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
47 |
-
|
48 |
-
generation_speed = 0
|
49 |
-
|
50 |
-
def execution_time_calculator(start_time, log=True):
|
51 |
-
delta = time.time() - start_time
|
52 |
-
if log:
|
53 |
-
print("--- %s seconds ---" % (delta))
|
54 |
-
return delta
|
55 |
-
|
56 |
-
def token_per_second_calculator(tokens_count, time_delta):
|
57 |
-
return tokens_count / time_delta
|
58 |
-
|
59 |
-
def generate(
|
60 |
-
message: str,
|
61 |
-
chat_history: list[tuple[str, str]],
|
62 |
-
max_new_tokens: int = 1024,
|
63 |
-
temperature: float = 0.6,
|
64 |
-
top_p: float = 0.9,
|
65 |
-
top_k: int = 50,
|
66 |
-
repetition_penalty: float = 1.2,
|
67 |
-
do_sample: bool = True,
|
68 |
-
) -> Iterator[str]:
|
69 |
-
conversation = []
|
70 |
-
conversation.append({"role": "system", "content": system_prompt})
|
71 |
-
|
72 |
-
# Add previous conversation to history (send the last two exchanges as context)
|
73 |
-
for user, assistant in chat_history[-2:]:
|
74 |
-
conversation.append({"role": "user", "content": user})
|
75 |
-
conversation.append({"role": "assistant", "content": assistant})
|
76 |
-
|
77 |
-
conversation.append({"role": "user", "content": message})
|
78 |
-
|
79 |
-
input_ids = tokenizer(conversation, return_tensors="pt", padding=True, truncation=True)
|
80 |
-
input_ids = input_ids.input_ids.to(model.device)
|
81 |
-
|
82 |
-
streamer = TextIteratorStreamer(
|
83 |
-
tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True
|
84 |
-
)
|
85 |
-
generate_kwargs = dict(
|
86 |
-
{"input_ids": input_ids},
|
87 |
-
streamer=streamer,
|
88 |
-
max_new_tokens=max_new_tokens,
|
89 |
-
do_sample=do_sample,
|
90 |
-
top_p=top_p,
|
91 |
-
top_k=top_k,
|
92 |
-
temperature=temperature,
|
93 |
-
num_beams=1,
|
94 |
-
repetition_penalty=repetition_penalty,
|
95 |
-
)
|
96 |
-
|
97 |
-
start_time = time.time()
|
98 |
-
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
99 |
-
t.start()
|
100 |
-
|
101 |
-
outputs = []
|
102 |
-
sum_tokens = 0
|
103 |
-
for text in streamer:
|
104 |
-
num_tokens = len(tokenizer.tokenize(text))
|
105 |
-
sum_tokens += num_tokens
|
106 |
-
outputs.append(text)
|
107 |
-
yield "".join(outputs)
|
108 |
-
|
109 |
-
time_delta = execution_time_calculator(start_time, log=False)
|
110 |
-
generation_speed = token_per_second_calculator(sum_tokens, time_delta)
|
111 |
-
|
112 |
-
# Define Gradio interface components
|
113 |
-
chatbot = gr.Chatbot(placeholder=PLACEHOLDER, scale=1, show_copy_button=True, height="68%")
|
114 |
-
chat_input = gr.Textbox(show_label=False, lines=2, placeholder="Enter your message", show_copy_button=True, scale=4)
|
115 |
-
submit_btn = gr.Button(variant="primary", value="Submit", size="sm", scale=1, elem_classes=["_button"])
|
116 |
-
|
117 |
-
chat_interface = gr.ChatInterface(
|
118 |
-
fn=generate,
|
119 |
-
additional_inputs_accordion=gr.Accordion(label="Additional Inputs", open=False),
|
120 |
-
additional_inputs=[
|
121 |
-
gr.Slider(
|
122 |
-
label="Max New Tokens",
|
123 |
-
minimum=1,
|
124 |
-
maximum=MAX_MAX_NEW_TOKENS,
|
125 |
-
step=1,
|
126 |
-
value=DEFAULT_MAX_NEW_TOKENS,
|
127 |
-
),
|
128 |
-
gr.Slider(
|
129 |
-
label="Temperature",
|
130 |
-
minimum=0.01,
|
131 |
-
maximum=4.0,
|
132 |
-
step=0.01,
|
133 |
-
value=0.6,
|
134 |
-
),
|
135 |
-
gr.Slider(
|
136 |
-
label="Top-p",
|
137 |
-
minimum=0.05,
|
138 |
-
maximum=1.0,
|
139 |
-
step=0.01,
|
140 |
-
value=0.9,
|
141 |
-
),
|
142 |
-
gr.Slider(
|
143 |
-
label="Top-k",
|
144 |
-
minimum=1,
|
145 |
-
maximum=1000,
|
146 |
-
step=1,
|
147 |
-
value=50,
|
148 |
-
),
|
149 |
-
gr.Slider(
|
150 |
-
label="Repetition Penalty",
|
151 |
-
minimum=1.0,
|
152 |
-
maximum=2.0,
|
153 |
-
step=0.05,
|
154 |
-
value=1.2,
|
155 |
-
),
|
156 |
-
gr.Dropdown(
|
157 |
-
label="Sampling",
|
158 |
-
choices=[False, True],
|
159 |
-
value=True
|
160 |
-
)
|
161 |
],
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
undo_btn="Undo",
|
168 |
-
clear_btn="Clear",
|
169 |
-
title="ISANG AI"
|
170 |
)
|
171 |
|
172 |
-
#
|
173 |
-
|
174 |
-
gr.Markdown(DESCRIPTION)
|
175 |
-
chat_interface.render()
|
176 |
-
|
177 |
-
if __name__ == "__main__":
|
178 |
-
demo.queue(max_size=20).launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
|
4 |
+
# Load model and tokenizer
|
5 |
+
model_name = "hosseinhimself/ISANG-v1.0-8B"
|
6 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Set hyperparameters for inference
|
10 |
+
def generate_response(input_text, max_tokens=1024, temperature=0.7, history=[]):
|
11 |
+
# Prepare the inputs for the model
|
12 |
+
prompt = f"Chat History: {history[-2:]}\nUser: {input_text}\nAI:"
|
13 |
+
|
14 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
15 |
+
output = model.generate(**inputs, max_new_tokens=max_tokens, temperature=temperature)
|
16 |
+
|
17 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
18 |
+
|
19 |
+
# Update history with the new conversation
|
20 |
+
history.append(f"User: {input_text}")
|
21 |
+
history.append(f"AI: {response}")
|
22 |
+
|
23 |
+
return response, history
|
24 |
+
|
25 |
+
# Gradio interface
|
26 |
+
iface = gr.Interface(
|
27 |
+
fn=generate_response,
|
28 |
+
inputs=[
|
29 |
+
gr.Textbox(label="Your Message", placeholder="Type your message here..."),
|
30 |
+
gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max Tokens"),
|
31 |
+
gr.Slider(minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
32 |
+
gr.State(value=[])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
],
|
34 |
+
outputs=[gr.Textbox(label="AI Response"), gr.State()],
|
35 |
+
title="ISANG Chatbot",
|
36 |
+
description="A chatbot powered by ISANG-v1.0-8B model. Chat with me!",
|
37 |
+
theme="huggingface",
|
38 |
+
live=True
|
|
|
|
|
|
|
39 |
)
|
40 |
|
41 |
+
# Launch the interface
|
42 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|