Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,45 @@
|
|
| 1 |
-
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# Nowy, l偶ejszy model
|
| 9 |
-
model_name = "distilbert/distilgpt2"
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
output = model.generate(
|
| 22 |
input_ids,
|
| 23 |
-
|
| 24 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 25 |
do_sample=True,
|
| 26 |
-
top_k=
|
| 27 |
-
top_p=
|
| 28 |
-
temperature=
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 32 |
-
reply = decoded[len(history):].split("U偶ytkownik:")[0].strip()
|
| 33 |
-
history += f" {reply}\n"
|
| 34 |
-
return history, history
|
| 35 |
-
|
| 36 |
-
with gr.Blocks() as demo:
|
| 37 |
-
gr.Markdown("# 馃 Polski Chatbot AI (DistilGPT2)")
|
| 38 |
-
|
| 39 |
-
chat_output = gr.Textbox(label="Historia rozmowy", lines=15, interactive=False)
|
| 40 |
-
user_input = gr.Textbox(label="Wpisz wiadomo艣膰")
|
| 41 |
-
|
| 42 |
-
top_k = gr.Slider(0, 100, value=50, step=1, label="Top-k")
|
| 43 |
-
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p")
|
| 44 |
-
temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")
|
| 45 |
-
|
| 46 |
-
history_state = gr.State("")
|
| 47 |
-
|
| 48 |
-
send_btn = gr.Button("Wy艣lij")
|
| 49 |
-
send_btn.click(
|
| 50 |
-
chatbot_response,
|
| 51 |
-
inputs=[user_input, history_state, top_k, top_p, temperature],
|
| 52 |
-
outputs=[chat_output, history_state]
|
| 53 |
)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Szybszy i sensowny model
|
| 7 |
+
model_name = "tiiuae/falcon-rw-1b"
|
|
|
|
|
|
|
|
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 10 |
|
| 11 |
+
def generate_response(user_input, history):
|
| 12 |
+
# Kontrola historii, ostatnie 3 interakcje
|
| 13 |
+
short_history = history[-3:] if history else []
|
| 14 |
+
|
| 15 |
+
# Budowanie promptu
|
| 16 |
+
prompt = ""
|
| 17 |
+
for human, bot in short_history:
|
| 18 |
+
prompt += f"User: {human}\nAI: {bot}\n"
|
| 19 |
+
prompt += f"User: {user_input}\nAI:"
|
| 20 |
+
|
| 21 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt", truncation=True, max_length=1024)
|
| 22 |
output = model.generate(
|
| 23 |
input_ids,
|
| 24 |
+
max_new_tokens=80,
|
|
|
|
| 25 |
do_sample=True,
|
| 26 |
+
top_k=50,
|
| 27 |
+
top_p=0.95,
|
| 28 |
+
temperature=0.7,
|
| 29 |
+
pad_token_id=tokenizer.eos_token_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
+
output_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 33 |
+
reply = output_text[len(prompt):].split("User:")[0].strip()
|
| 34 |
+
history.append((user_input, reply))
|
| 35 |
+
return reply, history
|
| 36 |
|
| 37 |
+
iface = gr.Interface(
|
| 38 |
+
fn=generate_response,
|
| 39 |
+
inputs=[gr.Textbox(label="Twoje pytanie"), gr.State([])],
|
| 40 |
+
outputs=[gr.Textbox(label="Odpowied藕 AI"), gr.State([])],
|
| 41 |
+
title="馃 Polski Chatbot AI",
|
| 42 |
+
description="Chatbot dzia艂aj膮cy na bazie modelu Falcon-RW-1B. Zadaj pytanie, a AI postara si臋 odpowiedzie膰 m膮drze i szybko."
|
| 43 |
+
)
|
| 44 |
|
| 45 |
+
iface.launch()
|
|
|