Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,36 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
import torch
|
| 5 |
|
| 6 |
-
# Szybszy i
|
| 7 |
-
model_name = "tiiuae/falcon-rw-1b"
|
| 8 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 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 |
-
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()
|
|
|
|
|
|
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|