sandz7 commited on
Commit
dbe650f
1 Parent(s): 87d044e

launched loki

Browse files
Files changed (2) hide show
  1. app.py +141 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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;">Amphisbeana 🐍</h1>
15
+ <p>This uses Llama 3 and GPT-4o as generation, both of these make the final generation. <a href="https://huggingface.co/meta-llama/Meta-Llama-3-8B"><b>Llama3-8b</b></a>and <a href="https://platform.openai.com/docs/models/gpt-4o"><b>GPT-4o</b></a></p>
16
+ </div>
17
+ '''
18
+
19
+ LICENSE = """
20
+ <p/>
21
+ ---
22
+ Built with Meta Llama 3
23
+ """
24
+
25
+ PLACEHOLDER = """
26
+ <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
27
+ <img src="https://ysharma-dummy-chat-app.hf.space/file=/tmp/gradio/8e75e61cc9bab22b7ce3dec85ab0e6db1da5d107/Meta_lockup_positive%20primary_RGB.jpg" style="width: 80%; max-width: 550px; height: auto; opacity: 0.55; ">
28
+ <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Meta llama3</h1>
29
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
30
+ </div>
31
+ """
32
+
33
+
34
+ css = """
35
+ h1 {
36
+ text-align: center;
37
+ display: block;
38
+ }
39
+ #duplicate-button {
40
+ margin: auto;
41
+ color: white;
42
+ background: #1565c0;
43
+ border-radius: 100vh;
44
+ }
45
+ """
46
+
47
+ # Load the tokenizer and model
48
+ tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
49
+ model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto") # to("cuda:0")
50
+ terminators = [
51
+ tokenizer.eos_token_id,
52
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
53
+ ]
54
+
55
+ @spaces.GPU(duration=120)
56
+ def chat_llama3_8b(message: str,
57
+ history: list,
58
+ temperature: float,
59
+ max_new_tokens: int
60
+ ) -> str:
61
+ """
62
+ Generate a streaming response using the llama3-8b model.
63
+ Args:
64
+ message (str): The input message.
65
+ history (list): The conversation history used by ChatInterface.
66
+ temperature (float): The temperature for generating the response.
67
+ max_new_tokens (int): The maximum number of new tokens to generate.
68
+ Returns:
69
+ str: The generated response.
70
+ """
71
+ conversation = []
72
+ for user, assistant in history:
73
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
74
+ conversation.append({"role": "user", "content": message})
75
+
76
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
77
+
78
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
79
+
80
+ generate_kwargs = dict(
81
+ input_ids= input_ids,
82
+ streamer=streamer,
83
+ max_new_tokens=max_new_tokens,
84
+ do_sample=True,
85
+ temperature=temperature,
86
+ eos_token_id=terminators,
87
+ )
88
+ # This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
89
+ if temperature == 0:
90
+ generate_kwargs['do_sample'] = False
91
+
92
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
93
+ t.start()
94
+
95
+ outputs = []
96
+ for text in streamer:
97
+ outputs.append(text)
98
+ #print(outputs)
99
+ yield "".join(outputs)
100
+
101
+
102
+ # Gradio block
103
+ chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
104
+
105
+ with gr.Blocks(fill_height=True, css=css) as demo:
106
+
107
+ gr.Markdown(DESCRIPTION)
108
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
109
+ gr.ChatInterface(
110
+ fn=chat_llama3_8b,
111
+ chatbot=chatbot,
112
+ fill_height=True,
113
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
114
+ additional_inputs=[
115
+ gr.Slider(minimum=0,
116
+ maximum=1,
117
+ step=0.1,
118
+ value=0.95,
119
+ label="Temperature",
120
+ render=False),
121
+ gr.Slider(minimum=128,
122
+ maximum=4096,
123
+ step=1,
124
+ value=512,
125
+ label="Max new tokens",
126
+ render=False ),
127
+ ],
128
+ examples=[
129
+ ['How to setup a human base on Mars? Give short answer.'],
130
+ ['Explain theory of relativity to me like I’m 8 years old.'],
131
+ ['What is 9,000 * 9,000?'],
132
+ ['Write a pun-filled happy birthday message to my friend Alex.'],
133
+ ['Justify why a penguin might make a good king of the jungle.']
134
+ ],
135
+ cache_examples=False,
136
+ )
137
+
138
+ gr.Markdown(LICENSE)
139
+
140
+ if __name__ == "__main__":
141
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ accelerate
2
+ transformers
3
+ SentencePiece