julienlesbegueries commited on
Commit
7510444
1 Parent(s): f60f5dc

try to chatbot ia

Browse files
Files changed (1) hide show
  1. app.py +22 -0
app.py CHANGED
@@ -2,6 +2,19 @@ import gradio as gr
2
  import os
3
  import time
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def greet(name):
6
  return "Hello " + name + "!!"
7
 
@@ -16,12 +29,21 @@ def print_like_dislike(x: gr.LikeData):
16
  print(x.index, x.value, x.liked)
17
 
18
 
 
 
 
 
 
 
 
19
  def add_text(history, text):
20
  history = history + [(text, None)]
 
21
  return history, gr.Textbox(value="", interactive=False)
22
 
23
 
24
  def add_file(history, file):
 
25
  history = history + [((file.name,), None)]
26
  return history
27
 
 
2
  import os
3
  import time
4
 
5
+ import torch
6
+ from transformers import pipeline
7
+
8
+ pipe = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.bfloat16, device_map="auto")
9
+
10
+ messages = [
11
+ {
12
+ "role": "system",
13
+ "content": "You are a friendly chatbot who always responds in the style of a pirate",
14
+ },
15
+ # {"role": "user", "content": "How many helicopters can a human eat in one sitting?"},
16
+ ]
17
+
18
  def greet(name):
19
  return "Hello " + name + "!!"
20
 
 
29
  print(x.index, x.value, x.liked)
30
 
31
 
32
+ def add_ia_text(history, text):
33
+ messages.append({"role": "user", "content": text})
34
+ prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
35
+ outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
36
+ history = history + ["IA: "+(outputs[0]["generated_text"], None)] # type: ignore
37
+
38
+
39
  def add_text(history, text):
40
  history = history + [(text, None)]
41
+ add_ia_text(history, text)
42
  return history, gr.Textbox(value="", interactive=False)
43
 
44
 
45
  def add_file(history, file):
46
+
47
  history = history + [((file.name,), None)]
48
  return history
49