File size: 1,636 Bytes
ea0182e
 
69a0b19
ea0182e
 
69a0b19
ea0182e
 
 
69a0b19
ea0182e
 
 
 
 
 
69a0b19
ea0182e
69a0b19
ea0182e
 
 
69a0b19
ea0182e
 
69a0b19
ea0182e
69a0b19
ea0182e
 
69a0b19
ea0182e
 
 
 
 
 
 
 
 
 
 
69a0b19
ea0182e
 
69a0b19
ea0182e
 
69a0b19
ea0182e
 
1
2
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import warnings
warnings.filterwarnings("ignore")

import logging
logging.getLogger("streamlit").setLevel(logging.ERROR)

import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

@st.cache_resource
def load_model():
    model_name = "radlab/polish-gpt2-small-v2"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForCausalLM.from_pretrained(model_name)
    return tokenizer, model

tokenizer, model = load_model()

st.set_page_config(page_title="Polski Chatbot AI", page_icon="🤖")
st.title("🤖 Polski Chatbot AI")
st.caption("Model: radlab/polish-gpt2-small-v2")

if "history" not in st.session_state:
    st.session_state.history = ""

user_input = st.text_input("Wpisz wiadomość:", "")

if st.button("Wyślij") and user_input.strip() != "":
    st.session_state.history += f"Użytkownik: {user_input}\nAI:"

    input_ids = tokenizer.encode(st.session_state.history, return_tensors="pt", truncation=True, max_length=1024)
    output = model.generate(
        input_ids,
        max_length=input_ids.shape[1] + 80,
        pad_token_id=tokenizer.eos_token_id,
        do_sample=True,
        top_k=50,
        top_p=0.95,
        temperature=0.7
    )
    output_text = tokenizer.decode(output[0], skip_special_tokens=True)

    model_reply = output_text[len(st.session_state.history):].split("Użytkownik:")[0].strip()
    st.session_state.history += f" {model_reply}\n"

st.subheader("🗨️ Historia rozmów")
st.text_area("📖", st.session_state.history.strip(), height=300)

if st.button("🧹 Wyczyść historię"):
    st.session_state.history = ""