Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,12 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStream
|
|
| 6 |
import torch
|
| 7 |
|
| 8 |
# Carregar modelo local
|
| 9 |
-
model_id = "lambdaindie/lambda-1v-1B"
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
| 12 |
model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
model.eval()
|
| 14 |
|
|
@@ -45,10 +48,7 @@ textarea, input, button, select {
|
|
| 45 |
|
| 46 |
theme = gr.themes.Base(
|
| 47 |
primary_hue="gray",
|
| 48 |
-
font=[
|
| 49 |
-
gr.themes.GoogleFont("JetBrains Mono"),
|
| 50 |
-
"monospace"
|
| 51 |
-
]
|
| 52 |
).set(
|
| 53 |
body_background_fill="#111",
|
| 54 |
body_text_color="#e0e0e0",
|
|
@@ -59,21 +59,21 @@ theme = gr.themes.Base(
|
|
| 59 |
block_title_text_color="#fff"
|
| 60 |
)
|
| 61 |
|
| 62 |
-
# Flag
|
| 63 |
stop_signal = False
|
| 64 |
|
| 65 |
def stop_stream():
|
| 66 |
global stop_signal
|
| 67 |
stop_signal = True
|
| 68 |
|
| 69 |
-
def respond(
|
| 70 |
global stop_signal
|
| 71 |
stop_signal = False
|
| 72 |
|
| 73 |
-
#
|
| 74 |
prompt = ""
|
| 75 |
if system_message:
|
| 76 |
-
prompt +=
|
| 77 |
|
| 78 |
for msg in history:
|
| 79 |
role = msg["role"]
|
|
@@ -83,11 +83,11 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 83 |
elif role == "assistant":
|
| 84 |
prompt += f"Assistant: {content}\n"
|
| 85 |
|
| 86 |
-
prompt +=
|
| 87 |
|
| 88 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 89 |
-
|
| 90 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
|
|
|
| 91 |
generation_kwargs = dict(
|
| 92 |
**inputs,
|
| 93 |
streamer=streamer,
|
|
@@ -107,14 +107,18 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 107 |
if stop_signal:
|
| 108 |
break
|
| 109 |
output += token
|
| 110 |
-
yield {"role": "assistant", "content": output}
|
| 111 |
|
| 112 |
end = time.time()
|
| 113 |
-
yield
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# Interface
|
| 116 |
with gr.Blocks(css=css, theme=theme) as app:
|
| 117 |
chatbot = gr.Chatbot(label="λ", type="messages")
|
|
|
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
msg = gr.Textbox(label="Mensagem")
|
|
@@ -127,16 +131,14 @@ with gr.Blocks(css=css, theme=theme) as app:
|
|
| 127 |
temperature = gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature")
|
| 128 |
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
def user_message_submit(user_msg, chat_history):
|
| 133 |
if user_msg:
|
| 134 |
chat_history = chat_history + [{"role": "user", "content": user_msg}]
|
| 135 |
return "", chat_history
|
| 136 |
|
| 137 |
-
send_btn.click(fn=
|
| 138 |
-
.then(fn=respond, inputs=[
|
| 139 |
|
| 140 |
stop_btn.click(fn=stop_stream, inputs=[], outputs=[])
|
| 141 |
|
| 142 |
-
app.launch(share=True
|
|
|
|
| 6 |
import torch
|
| 7 |
|
| 8 |
# Carregar modelo local
|
| 9 |
+
model_id = "lambdaindie/lambda-1v-1B"
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
model_id,
|
| 13 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 14 |
+
)
|
| 15 |
model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
model.eval()
|
| 17 |
|
|
|
|
| 48 |
|
| 49 |
theme = gr.themes.Base(
|
| 50 |
primary_hue="gray",
|
| 51 |
+
font=[gr.themes.GoogleFont("JetBrains Mono"), "monospace"]
|
|
|
|
|
|
|
|
|
|
| 52 |
).set(
|
| 53 |
body_background_fill="#111",
|
| 54 |
body_text_color="#e0e0e0",
|
|
|
|
| 59 |
block_title_text_color="#fff"
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# Flag de parada
|
| 63 |
stop_signal = False
|
| 64 |
|
| 65 |
def stop_stream():
|
| 66 |
global stop_signal
|
| 67 |
stop_signal = True
|
| 68 |
|
| 69 |
+
def respond(history, system_message, max_tokens, temperature, top_p):
|
| 70 |
global stop_signal
|
| 71 |
stop_signal = False
|
| 72 |
|
| 73 |
+
# Construir prompt
|
| 74 |
prompt = ""
|
| 75 |
if system_message:
|
| 76 |
+
prompt += system_message + "\n\n"
|
| 77 |
|
| 78 |
for msg in history:
|
| 79 |
role = msg["role"]
|
|
|
|
| 83 |
elif role == "assistant":
|
| 84 |
prompt += f"Assistant: {content}\n"
|
| 85 |
|
| 86 |
+
prompt += "Assistant:"
|
| 87 |
|
| 88 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
|
| 89 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 90 |
+
|
| 91 |
generation_kwargs = dict(
|
| 92 |
**inputs,
|
| 93 |
streamer=streamer,
|
|
|
|
| 107 |
if stop_signal:
|
| 108 |
break
|
| 109 |
output += token
|
| 110 |
+
yield history + [{"role": "assistant", "content": output}]
|
| 111 |
|
| 112 |
end = time.time()
|
| 113 |
+
yield history + [
|
| 114 |
+
{"role": "assistant", "content": output},
|
| 115 |
+
{"role": "system", "content": f"Pensou por {end - start:.1f} segundos"}
|
| 116 |
+
]
|
| 117 |
|
| 118 |
# Interface
|
| 119 |
with gr.Blocks(css=css, theme=theme) as app:
|
| 120 |
chatbot = gr.Chatbot(label="λ", type="messages")
|
| 121 |
+
state = gr.State([])
|
| 122 |
|
| 123 |
with gr.Row():
|
| 124 |
msg = gr.Textbox(label="Mensagem")
|
|
|
|
| 131 |
temperature = gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature")
|
| 132 |
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 133 |
|
| 134 |
+
def handle_user_msg(user_msg, chat_history):
|
|
|
|
|
|
|
| 135 |
if user_msg:
|
| 136 |
chat_history = chat_history + [{"role": "user", "content": user_msg}]
|
| 137 |
return "", chat_history
|
| 138 |
|
| 139 |
+
send_btn.click(fn=handle_user_msg, inputs=[msg, state], outputs=[msg, state])\
|
| 140 |
+
.then(fn=respond, inputs=[state, system_message, max_tokens, temperature, top_p], outputs=[chatbot, state])
|
| 141 |
|
| 142 |
stop_btn.click(fn=stop_stream, inputs=[], outputs=[])
|
| 143 |
|
| 144 |
+
app.launch(share=True
|