aaabiao commited on
Commit
2560819
·
verified ·
1 Parent(s): 069e918

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -8
app.py CHANGED
@@ -1,14 +1,108 @@
 
 
 
 
1
  import gradio as gr
2
  import spaces
3
  import torch
4
- from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- tokenizer = AutoTokenizer.from_pretrained("TIGER-Lab/MAmmoTH2-8B-Plus", trust_remote_code=True)
7
- model = AutoModelForCausalLM.from_pretrained("TIGER-Lab/MAmmoTH2-8B-Plus", trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- def predict(input_text):
10
- outputs = model.generate(input_text, max_length=50, do_sample=True)
11
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
12
 
13
- demo = gr.Interface(fn=predict, inputs="text", outputs="text")
14
- demo.launch()
 
1
+ import os
2
+ from threading import Thread
3
+ from typing import Iterator
4
+
5
  import gradio as gr
6
  import spaces
7
  import torch
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
9
+
10
+ MAX_MAX_NEW_TOKENS = 2048
11
+ DEFAULT_MAX_NEW_TOKENS = 1024
12
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
13
+
14
+ if torch.cuda.is_available():
15
+ model_id = "TIGER-Lab/MAmmoTH2-8B-Plus"
16
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
17
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
18
+
19
+ @spaces.GPU
20
+ def generate(
21
+ message: str,
22
+ chat_history: list[tuple[str, str]],
23
+ system_prompt: str,
24
+ max_new_tokens: int = 1024,
25
+ temperature: float = 0.7,
26
+ top_p: float = 1.0,
27
+ repetition_penalty: float = 1.1,
28
+ ) -> Iterator[str]:
29
+ conversation = []
30
+ if system_prompt:
31
+ conversation.append({"role": "system", "content": system_prompt})
32
+ for user, assistant in chat_history:
33
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
34
+ conversation.append({"role": "user", "content": message})
35
+
36
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
37
+ if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
38
+ input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
39
+ gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
40
+ input_ids = input_ids.to(model.device)
41
+
42
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
43
+ generate_kwargs = dict(
44
+ {"input_ids": input_ids},
45
+ streamer=streamer,
46
+ max_new_tokens=max_new_tokens,
47
+ do_sample=True,
48
+ top_p=top_p,
49
+ temperature=temperature,
50
+ num_beams=1,
51
+ repetition_penalty=repetition_penalty,
52
+ )
53
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
54
+ t.start()
55
+
56
+ outputs = []
57
+ for text in streamer:
58
+ outputs.append(text)
59
+ yield "".join(outputs)
60
 
61
+ chat_interface = gr.ChatInterface(
62
+ fn=generate,
63
+ additional_inputs=[
64
+ gr.Textbox(label="System prompt", lines=6),
65
+ gr.Slider(
66
+ label="Max new tokens",
67
+ minimum=1,
68
+ maximum=MAX_MAX_NEW_TOKENS,
69
+ step=1,
70
+ value=DEFAULT_MAX_NEW_TOKENS,
71
+ ),
72
+ gr.Slider(
73
+ label="Temperature",
74
+ minimum=0.01,
75
+ maximum=1.0,
76
+ step=0.01,
77
+ value=0.7,
78
+ ),
79
+ gr.Slider(
80
+ label="Top-p (nucleus sampling)",
81
+ minimum=0.05,
82
+ maximum=1.0,
83
+ step=0.01,
84
+ value=1.0,
85
+ ),
86
+ gr.Slider(
87
+ label="Repetition penalty",
88
+ minimum=1.0,
89
+ maximum=2.0,
90
+ step=0.05,
91
+ value=1.1,
92
+ ),
93
+ ],
94
+ stop_btn=None,
95
+ examples=[
96
+ ["Hello there! How are you doing?"],
97
+ ["Can you explain briefly to me what is the Python programming language?"],
98
+ ["Explain the plot of Cinderella in a sentence."],
99
+ ["How many hours does it take a man to eat a Helicopter?"],
100
+ ["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
101
+ ],
102
+ )
103
 
104
+ with gr.Blocks(css="style.css") as demo:
105
+ chat_interface.render()
 
106
 
107
+ if __name__ == "__main__":
108
+ demo.queue(max_size=20).launch()