mtb2023 commited on
Commit
5e8df2b
·
1 Parent(s): 18fd1ed

adjust model

Browse files
Files changed (1) hide show
  1. app.py +60 -13
app.py CHANGED
@@ -1,27 +1,74 @@
1
  from openai import OpenAI
2
  import gradio as gr
 
 
 
3
 
4
- api_key = "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"
5
- client = OpenAI(api_key=api_key, base_url="https://api.perplexity.ai")
6
 
7
- def predict(message, history):
8
- history_openai_format = []
9
  #history_openai_format.append({"role": "system", "content": "Answer the questions in japanese only"})
10
 
11
  #for human, assistant in history:
12
  #history_openai_format.append({"role": "user", "content": human })
13
  #history_openai_format.append({"role": "assistant", "content":assistant})
14
- history_openai_format.append({"role": "user", "content": message})
15
 
16
- response = client.chat.completions.create(model='sonar-small-chat',
17
- messages= history_openai_format,
18
- temperature=1.0,
19
- stream=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  partial_message = ""
22
- for chunk in response:
23
- if chunk.choices[0].delta.content is not None:
24
- partial_message = partial_message + chunk.choices[0].delta.content
25
- yield partial_message
26
 
27
  gr.ChatInterface(predict).launch()
 
1
  from openai import OpenAI
2
  import gradio as gr
3
+ import torch
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer, StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
5
+ from threading import Thread
6
 
7
+ #api_key = "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"
8
+ #client = OpenAI(api_key=api_key, base_url="https://api.perplexity.ai")
9
 
10
+ #def predict(message, history):
11
+ # history_openai_format = []
12
  #history_openai_format.append({"role": "system", "content": "Answer the questions in japanese only"})
13
 
14
  #for human, assistant in history:
15
  #history_openai_format.append({"role": "user", "content": human })
16
  #history_openai_format.append({"role": "assistant", "content":assistant})
17
+ # history_openai_format.append({"role": "user", "content": message})
18
 
19
+ # response = client.chat.completions.create(model='sonar-small-chat',
20
+ # messages= history_openai_format,
21
+ # temperature=1.0,
22
+ # stream=True)
23
+
24
+ # partial_message = ""
25
+ # for chunk in response:
26
+ # if chunk.choices[0].delta.content is not None:
27
+ # partial_message = partial_message + chunk.choices[0].delta.content
28
+ # yield partial_message
29
+
30
+ #gr.ChatInterface(predict).launch()
31
+
32
+
33
+ tokenizer = AutoTokenizer.from_pretrained("togethercomputer/RedPajama-INCITE-Chat-3B-v1")
34
+ model = AutoModelForCausalLM.from_pretrained("togethercomputer/RedPajama-INCITE-Chat-3B-v1", torch_dtype=torch.float16)
35
+ model = model.to('cuda:0')
36
+
37
+ class StopOnTokens(StoppingCriteria):
38
+ def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
39
+ stop_ids = [29, 0]
40
+ for stop_id in stop_ids:
41
+ if input_ids[0][-1] == stop_id:
42
+ return True
43
+ return False
44
+
45
+ def predict(message, history):
46
+ history_transformer_format = history + [[message, ""]]
47
+ stop = StopOnTokens()
48
+
49
+ messages = "".join(["".join(["\n<human>:"+item[0], "\n<bot>:"+item[1]])
50
+ for item in history_transformer_format])
51
+
52
+ model_inputs = tokenizer([messages], return_tensors="pt").to("cuda")
53
+ streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
54
+ generate_kwargs = dict(
55
+ model_inputs,
56
+ streamer=streamer,
57
+ max_new_tokens=1024,
58
+ do_sample=True,
59
+ top_p=0.95,
60
+ top_k=1000,
61
+ temperature=1.0,
62
+ num_beams=1,
63
+ stopping_criteria=StoppingCriteriaList([stop])
64
+ )
65
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
66
+ t.start()
67
 
68
  partial_message = ""
69
+ for new_token in streamer:
70
+ if new_token != '<':
71
+ partial_message += new_token
72
+ yield partial_message
73
 
74
  gr.ChatInterface(predict).launch()