umair894 commited on
Commit
5ce6989
·
verified ·
1 Parent(s): c4bc9a3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -0
app.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import spaces
4
+ from transformers import GemmaTokenizer, AutoModelForCausalLM
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
6
+ from threading import Thread
7
+
8
+ # Set an environment variable
9
+ #HF_TOKEN = os.environ.get("HF_TOKEN", None)
10
+
11
+
12
+ DESCRIPTION = '''
13
+ <div>
14
+ <h1 style="text-align: center;">Llama3 8B Fine-tuned</h1>
15
+ '''
16
+
17
+ # LICENSE = """
18
+ # """
19
+
20
+ PLACEHOLDER = """
21
+ <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
22
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">I am Vikk, AI legel Assistant, Ask me anything</p>
23
+ </div>
24
+ """
25
+
26
+
27
+ css = """
28
+ h1 {
29
+ text-align: center;
30
+ display: block;
31
+ }
32
+ #duplicate-button {
33
+ margin: auto;
34
+ color: white;
35
+ background: #1565c0;
36
+ border-radius: 100vh;
37
+ }
38
+ """
39
+
40
+ # Load the tokenizer and model
41
+ tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
42
+ model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="cuda:0") # to("auto")
43
+ terminators = [
44
+ tokenizer.eos_token_id,
45
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
46
+ ]
47
+
48
+ #@spaces.GPU(duration=120)
49
+ def chat_llama3_8b(message: str,
50
+ history: list,
51
+ temperature: float,
52
+ max_new_tokens: int
53
+ ) -> str:
54
+ """
55
+ Generate a streaming response using the llama3-8b model.
56
+ Args:
57
+ message (str): The input message.
58
+ history (list): The conversation history used by ChatInterface.
59
+ temperature (float): The temperature for generating the response.
60
+ max_new_tokens (int): The maximum number of new tokens to generate.
61
+ Returns:
62
+ str: The generated response.
63
+ """
64
+ conversation = []
65
+ for user, assistant in history:
66
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
67
+ conversation.append({"role": "user", "content": message})
68
+
69
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
70
+
71
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
72
+
73
+ generate_kwargs = dict(
74
+ input_ids= input_ids,
75
+ streamer=streamer,
76
+ max_new_tokens=max_new_tokens,
77
+ do_sample=True,
78
+ #temperature=temperature,
79
+ eos_token_id=terminators,
80
+ )
81
+ # This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
82
+ if temperature == 0:
83
+ generate_kwargs['do_sample'] = False
84
+
85
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
86
+ t.start()
87
+
88
+ outputs = []
89
+ for text in streamer:
90
+ outputs.append(text)
91
+ #print(outputs)
92
+ yield "".join(outputs)
93
+
94
+
95
+ # Gradio block
96
+ chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
97
+
98
+ with gr.Blocks(fill_height=True, css=css) as demo:
99
+
100
+ gr.Markdown(DESCRIPTION)
101
+ #gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
102
+ gr.ChatInterface(
103
+ fn=chat_llama3_8b,
104
+ chatbot=chatbot,
105
+ fill_height=True,
106
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
107
+ additional_inputs=[
108
+ # gr.Slider(minimum=0,
109
+ # maximum=1,
110
+ # step=0.1,
111
+ # value=0.95,
112
+ # label="Temperature",
113
+ # render=False),
114
+ gr.Slider(minimum=128,
115
+ maximum=4096,
116
+ step=1,
117
+ value=512,
118
+ label="Max new tokens",
119
+ render=False ),
120
+ ],
121
+ examples=[
122
+ ['I got a ticket.'],
123
+ ],
124
+ cache_examples=False,
125
+ )
126
+
127
+ #gr.Markdown(LICENSE)
128
+
129
+ if __name__ == "__main__":
130
+ demo.launch()