Samuel4677 commited on
Commit
4c03c86
·
verified ·
1 Parent(s): 2751646

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -34
app.py CHANGED
@@ -1,36 +1,29 @@
 
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
 
4
- model_id = "tiiuae/falcon-rw-1b" # Szybszy i sensowniejszy model
5
-
6
- tokenizer = AutoTokenizer.from_pretrained(model_id)
7
- model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
8
- generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
9
-
10
- chat_history = []
11
-
12
- def chat(user_input):
13
- global chat_history
14
- chat_history.append(f"User: {user_input}")
15
- prompt = "\n".join(chat_history) + "\nAI:"
16
-
17
- response = generator(prompt, max_new_tokens=80, do_sample=True, top_k=40, top_p=0.9, temperature=0.7)[0]["generated_text"]
18
- reply = response[len(prompt):].strip().split("\nUser:")[0]
19
-
20
- chat_history.append(f"AI: {reply}")
21
- return reply
22
-
23
- with gr.Blocks() as demo:
24
- gr.Markdown("## 🤖 Polski Chatbot AI – Szybki i Inteligentny")
25
- chatbox = gr.Chatbot()
26
- msg = gr.Textbox(label="Twoja wiadomość")
27
- send_btn = gr.Button("Wyślij")
28
-
29
- def respond(user_input, history=[]):
30
- reply = chat(user_input)
31
- history.append((user_input, reply))
32
- return history, ""
33
-
34
- send_btn.click(respond, inputs=[msg, chatbox], outputs=[chatbox, msg])
35
-
36
- demo.launch()
 
1
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
  import gradio as gr
 
3
 
4
+ # Załaduj model
5
+ model_name = "google/mt5-small"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
+
9
+ # Funkcja odpowiadająca na pytanie
10
+ def answer_question(question):
11
+ input_text = f"Pytanie: {question} Odpowiedź:"
12
+ inputs = tokenizer.encode(input_text, return_tensors="pt")
13
+ output = model.generate(
14
+ inputs,
15
+ max_new_tokens=60,
16
+ do_sample=False,
17
+ temperature=0.3,
18
+ top_p=0.95
19
+ )
20
+ return tokenizer.decode(output[0], skip_special_tokens=True)
21
+
22
+ # Gradio UI
23
+ gr.Interface(
24
+ fn=answer_question,
25
+ inputs=gr.Textbox(lines=2, placeholder="Zadaj pytanie..."),
26
+ outputs=gr.Textbox(),
27
+ title="🤖 Polski Chatbot AI",
28
+ description="Zadaj pytanie po polsku, a chatbot udzieli sensownej odpowiedzi"
29
+ ).launch()