Samuel4677 commited on
Commit
f2373c7
verified
1 Parent(s): 66d4184

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -54
app.py CHANGED
@@ -1,66 +1,45 @@
1
- import torch
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import gradio as gr
 
 
4
 
5
- # Optymalizacja oblicze艅
6
- torch.set_float32_matmul_precision('high')
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
- MAX_HISTORY = 800 # limit token贸w w historii
14
-
15
- def chatbot_response(user_input, history, top_k, top_p, temperature):
16
- history += f"U偶ytkownik: {user_input}\nAI:"
17
- input_ids = tokenizer.encode(history, return_tensors="pt", truncation=True, max_length=1024)
18
- if input_ids.shape[1] > MAX_HISTORY:
19
- input_ids = input_ids[:, -MAX_HISTORY:]
20
-
 
 
 
21
  output = model.generate(
22
  input_ids,
23
- max_length=input_ids.shape[1] + 80,
24
- pad_token_id=tokenizer.eos_token_id,
25
  do_sample=True,
26
- top_k=int(top_k),
27
- top_p=top_p,
28
- temperature=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
- clear_btn = gr.Button("馃Ч Wyczy艣膰 histori臋")
56
- clear_btn.click(lambda: ("", ""), outputs=[chat_output, history_state])
 
 
57
 
58
- gr.Markdown("## 馃攧 Szybkie pytania:")
59
- with gr.Row():
60
- gr.Button("Jak si臋 nazywasz?").click(
61
- lambda _: chatbot_response("Jak si臋 nazywasz?", "", 50, 0.9, 0.7), outputs=[chat_output, history_state])
62
- gr.Button("Czym si臋 zajmujesz?").click(
63
- lambda _: chatbot_response("Czym si臋 zajmujesz?", "", 50, 0.9, 0.7), outputs=[chat_output, history_state])
 
64
 
65
- if __name__ == "__main__":
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()