Jizzo commited on
Commit
7ab52dd
·
verified ·
1 Parent(s): d89e5a8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import gradio as gr
4
+
5
+ model_name = "LeoLM/leo-mistral-chat"
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
11
+ )
12
+ model = model.to("cuda" if torch.cuda.is_available() else "cpu")
13
+
14
+ def chat(user_input, history=[]):
15
+ # Baue den Prompt auf
16
+ prompt = ""
17
+ for user, bot in history:
18
+ prompt += f"User: {user}\nAssistant: {bot}\n"
19
+ prompt += f"User: {user_input}\nAssistant:"
20
+
21
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True).to(model.device)
22
+ output = model.generate(**inputs, max_new_tokens=256, do_sample=True, top_p=0.9, temperature=0.7, pad_token_id=tokenizer.eos_token_id)
23
+ decoded = tokenizer.decode(output[0], skip_special_tokens=True)
24
+ answer = decoded.split("Assistant:")[-1].strip()
25
+ history.append((user_input, answer))
26
+ return history, history
27
+
28
+ with gr.Blocks() as demo:
29
+ gr.Markdown("## 🤖 Leichtgewichtiger KI-Chat auf Deutsch")
30
+ chatbot = gr.Chatbot()
31
+ msg = gr.Textbox(label="Deine Nachricht")
32
+ state = gr.State([])
33
+
34
+ msg.submit(chat, [msg, state], [chatbot, state])
35
+
36
+ demo.launch()