Spaces:
Build error

akhilsu commited on
Commit
376f1df
·
verified ·
1 Parent(s): 2c7d642

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -61
app.py CHANGED
@@ -1,77 +1,69 @@
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;">Meta Llama3 8B</h1>
15
- <p>This Space demonstrates the instruction-tuned model <a href="https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct"><b>Meta Llama3 8b Chat</b></a>. Meta Llama3 is the new open LLM and comes in two sizes: 8b and 70b. Feel free to play with it, or duplicate to run privately!</p>
16
- <p>🔎 For more details about the Llama3 release and how to use the model with <code>transformers</code>, take a look <a href="https://huggingface.co/blog/llama3">at our blog post</a>.</p>
17
- <p>🦕 Looking for an even more powerful model? Check out the <a href="https://huggingface.co/chat/"><b>Hugging Chat</b></a> integration for Meta Llama 3 70b</p>
18
  </div>
19
  '''
20
 
21
  LICENSE = """
22
- <p/>
23
- ---
24
- Built with Meta Llama 3
 
25
  """
26
 
27
  PLACEHOLDER = """
28
  <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
29
- <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; ">
30
- <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Meta llama3</h1>
31
- <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
32
  </div>
33
  """
34
 
35
-
36
  css = """
37
- h1 {
38
- text-align: center;
39
- display: block;
 
 
40
  }
41
  #duplicate-button {
42
  margin: auto;
43
- color: white;
44
- background: #1565c0;
45
- border-radius: 100vh;
 
 
 
 
 
 
 
46
  }
47
  """
48
 
49
  # Load the tokenizer and model
50
- #tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
51
- #model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto") # to("cuda:0")
52
  tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B-Instruct")
53
- model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B-Instruct", device_map="auto") # to("cuda:0")
 
54
  terminators = [
55
  tokenizer.eos_token_id,
56
  tokenizer.convert_tokens_to_ids("<|eot_id|>")
57
  ]
58
 
59
  @spaces.GPU(duration=120)
60
- def chat_llama3_8b(message: str,
61
- history: list,
62
- temperature: float,
63
- max_new_tokens: int
64
- ) -> str:
65
- """
66
- Generate a streaming response using the llama3-8b model.
67
- Args:
68
- message (str): The input message.
69
- history (list): The conversation history used by ChatInterface.
70
- temperature (float): The temperature for generating the response.
71
- max_new_tokens (int): The maximum number of new tokens to generate.
72
- Returns:
73
- str: The generated response.
74
- """
75
  conversation = []
76
  for user, assistant in history:
77
  conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
@@ -82,14 +74,14 @@ def chat_llama3_8b(message: str,
82
  streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
83
 
84
  generate_kwargs = dict(
85
- input_ids= input_ids,
86
  streamer=streamer,
87
  max_new_tokens=max_new_tokens,
88
  do_sample=True,
89
  temperature=temperature,
90
  eos_token_id=terminators,
91
  )
92
- # This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
93
  if temperature == 0:
94
  generate_kwargs['do_sample'] = False
95
 
@@ -99,15 +91,12 @@ def chat_llama3_8b(message: str,
99
  outputs = []
100
  for text in streamer:
101
  outputs.append(text)
102
- #print(outputs)
103
  yield "".join(outputs)
104
-
105
 
106
  # Gradio block
107
- chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
108
 
109
  with gr.Blocks(fill_height=True, css=css) as demo:
110
-
111
  gr.Markdown(DESCRIPTION)
112
  gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
113
  gr.ChatInterface(
@@ -116,31 +105,20 @@ with gr.Blocks(fill_height=True, css=css) as demo:
116
  fill_height=True,
117
  additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
118
  additional_inputs=[
119
- gr.Slider(minimum=0,
120
- maximum=1,
121
- step=0.1,
122
- value=0.95,
123
- label="Temperature",
124
- render=False),
125
- gr.Slider(minimum=128,
126
- maximum=4096,
127
- step=1,
128
- value=512,
129
- label="Max new tokens",
130
- render=False ),
131
- ],
132
  examples=[
133
  ['How to setup a human base on Mars? Give short answer.'],
134
  ['Explain theory of relativity to me like I’m 8 years old.'],
135
  ['What is 9,000 * 9,000?'],
136
  ['Write a pun-filled happy birthday message to my friend Alex.'],
137
  ['Justify why a penguin might make a good king of the jungle.']
138
- ],
139
  cache_examples=False,
140
- )
141
 
142
  gr.Markdown(LICENSE)
143
 
144
  if __name__ == "__main__":
145
- demo.launch()
146
-
 
1
  import gradio as gr
2
  import os
3
  import spaces
 
4
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
5
  from threading import Thread
6
 
7
  # Set an environment variable
8
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
9
 
 
10
  DESCRIPTION = '''
11
+ <div style="background-color: #f9f9f9; padding: 20px; border-radius: 8px;">
12
+ <h1 style="text-align: center; font-family: Arial, sans-serif; color: #333;">Meta Llama3 8B</h1>
13
+ <p style="font-family: Arial, sans-serif; color: #555;">This Space demonstrates the instruction-tuned model <a href="https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct"><b>Meta Llama3 8b Chat</b></a>. Meta Llama3 is the new open LLM and comes in two sizes: 8b and 70b. Feel free to play with it, or duplicate to run privately!</p>
14
+ <p style="font-family: Arial, sans-serif; color: #555;">🔎 For more details about the Llama3 release and how to use the model with <code>transformers</code>, take a look <a href="https://huggingface.co/blog/llama3">at our blog post</a>.</p>
15
+ <p style="font-family: Arial, sans-serif; color: #555;">🦕 Looking for an even more powerful model? Check out the <a href="https://huggingface.co/chat/"><b>Hugging Chat</b></a> integration for Meta Llama 3 70b</p>
16
  </div>
17
  '''
18
 
19
  LICENSE = """
20
+ <p style="font-family: Arial, sans-serif; color: #999; text-align: center; padding-top: 20px;">---
21
+ Built with Meta Llama 3<br>
22
+ Developed by Akhil Sudhakaran
23
+ </p>
24
  """
25
 
26
  PLACEHOLDER = """
27
  <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
28
+ <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.75;">
29
+ <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.75; font-family: Arial, sans-serif; color: #444;">Meta Llama3</h1>
30
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65; font-family: Arial, sans-serif; color: #666;">Ask me anything...</p>
31
  </div>
32
  """
33
 
 
34
  css = """
35
+ body {
36
+ background-color: #ffffff;
37
+ }
38
+ h1, p {
39
+ font-family: Arial, sans-serif;
40
  }
41
  #duplicate-button {
42
  margin: auto;
43
+ color: #ffffff;
44
+ background: #007BFF;
45
+ border-radius: 100px;
46
+ padding: 10px 20px;
47
+ border: none;
48
+ cursor: pointer;
49
+ font-family: Arial, sans-serif;
50
+ }
51
+ #duplicate-button:hover {
52
+ background: #0056b3;
53
  }
54
  """
55
 
56
  # Load the tokenizer and model
 
 
57
  tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B-Instruct")
58
+ model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B-Instruct", device_map="auto")
59
+
60
  terminators = [
61
  tokenizer.eos_token_id,
62
  tokenizer.convert_tokens_to_ids("<|eot_id|>")
63
  ]
64
 
65
  @spaces.GPU(duration=120)
66
+ def chat_llama3_8b(message: str, history: list, temperature: float, max_new_tokens: int) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  conversation = []
68
  for user, assistant in history:
69
  conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
 
74
  streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
75
 
76
  generate_kwargs = dict(
77
+ input_ids=input_ids,
78
  streamer=streamer,
79
  max_new_tokens=max_new_tokens,
80
  do_sample=True,
81
  temperature=temperature,
82
  eos_token_id=terminators,
83
  )
84
+
85
  if temperature == 0:
86
  generate_kwargs['do_sample'] = False
87
 
 
91
  outputs = []
92
  for text in streamer:
93
  outputs.append(text)
 
94
  yield "".join(outputs)
 
95
 
96
  # Gradio block
97
+ chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
98
 
99
  with gr.Blocks(fill_height=True, css=css) as demo:
 
100
  gr.Markdown(DESCRIPTION)
101
  gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
102
  gr.ChatInterface(
 
105
  fill_height=True,
106
  additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
107
  additional_inputs=[
108
+ gr.Slider(minimum=0, maximum=1, step=0.1, value=0.95, label="Temperature", render=False),
109
+ gr.Slider(minimum=128, maximum=4096, step=1, value=512, label="Max new tokens", render=False),
110
+ ],
 
 
 
 
 
 
 
 
 
 
111
  examples=[
112
  ['How to setup a human base on Mars? Give short answer.'],
113
  ['Explain theory of relativity to me like I’m 8 years old.'],
114
  ['What is 9,000 * 9,000?'],
115
  ['Write a pun-filled happy birthday message to my friend Alex.'],
116
  ['Justify why a penguin might make a good king of the jungle.']
117
+ ],
118
  cache_examples=False,
119
+ )
120
 
121
  gr.Markdown(LICENSE)
122
 
123
  if __name__ == "__main__":
124
+ demo.launch()