sagar007 commited on
Commit
8b8d0cf
·
verified ·
1 Parent(s): 5232bb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -76
app.py CHANGED
@@ -1,88 +1,111 @@
1
  import gradio as gr
2
  import spaces
3
  import torch
4
- from transformers import AutoTokenizer, AutoModelForCausalLM
5
 
6
- # HTML template for custom UI
 
 
 
 
 
 
 
 
 
 
7
  HTML_TEMPLATE = """
8
  <style>
9
- body { background: linear-gradient(135deg, #f5f7fa, #c3cfe2); }
10
- #app-header {
11
- text-align: center;
12
- background: rgba(255, 255, 255, 0.8);
13
- padding: 20px;
14
- border-radius: 10px;
15
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
16
- position: relative;
17
- }
18
- #app-header h1 { color: #4CAF50; font-size: 2em; margin-bottom: 10px; }
19
- .concept { position: relative; transition: transform 0.3s; }
20
- .concept:hover { transform: scale(1.1); }
21
- .concept img {
22
- width: 100px;
23
- border-radius: 10px;
24
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
25
- }
26
- .concept-description {
27
- position: absolute;
28
- bottom: -30px;
29
- left: 50%;
30
- transform: translateX(-50%);
31
- background-color: #4CAF50;
32
- color: white;
33
- padding: 5px 10px;
34
- border-radius: 5px;
35
- opacity: 0;
36
- transition: opacity 0.3s;
37
- }
38
- .concept:hover .concept-description { opacity: 1; }
39
- .artifact {
40
- position: absolute;
41
- background: rgba(76, 175, 80, 0.1);
42
- border-radius: 50%;
43
- }
44
- .artifact.large { width: 300px; height: 300px; top: -50px; left: -150px; }
45
- .artifact.medium { width: 200px; height: 200px; bottom: -50px; right: -100px; }
46
- .artifact.small {
47
- width: 100px;
48
- height: 100px;
49
- top: 50%;
50
- left: 50%;
51
- transform: translate(-50%, -50%);
52
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  </style>
 
54
  <div id="app-header">
55
  <div class="artifact large"></div>
56
  <div class="artifact medium"></div>
57
  <div class="artifact small"></div>
58
- <h1>Llama-3.1-Storm-8B Text Generator</h1>
59
- <p>Generate text using the Llama-3.1-Storm-8B model by providing a prompt.</p>
60
- <div style="display: flex; justify-content: center; gap: 20px; margin-top: 20px;">
61
- <div class="concept">
62
- <img src="https://raw.githubusercontent.com/huggingface/huggingface.js/main/packages/inference/src/tasks/images/llama.png" alt="Llama">
63
- <div class="concept-description">Llama Model</div>
64
- </div>
65
- <div class="concept">
66
- <img src="https://raw.githubusercontent.com/huggingface/huggingface.js/main/packages/inference/src/tasks/images/language.png" alt="Language">
67
- <div class="concept-description">Natural Language Processing</div>
68
- </div>
69
- <div class="concept">
70
- <img src="https://raw.githubusercontent.com/huggingface/huggingface.js/main/packages/inference/src/tasks/images/text-generation.png" alt="Text Generation">
71
- <div class="concept-description">Text Generation</div>
72
- </div>
73
  </div>
74
  </div>
75
  """
76
 
77
- # Load the model and tokenizer
78
- model_name = "akjindal53244/Llama-3.1-Storm-8B"
79
- tokenizer = AutoTokenizer.from_pretrained(model_name)
80
- model = AutoModelForCausalLM.from_pretrained(
81
- model_name,
82
- torch_dtype=torch.bfloat16,
83
- device_map="auto"
84
- )
85
-
86
  @spaces.GPU(duration=120)
87
  def generate_text(prompt, max_length, temperature):
88
  messages = [
@@ -91,10 +114,8 @@ def generate_text(prompt, max_length, temperature):
91
  ]
92
  formatted_prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
93
 
94
- inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
95
-
96
- outputs = model.generate(
97
- **inputs,
98
  max_new_tokens=max_length,
99
  do_sample=True,
100
  temperature=temperature,
@@ -102,9 +123,8 @@ def generate_text(prompt, max_length, temperature):
102
  top_p=0.95,
103
  )
104
 
105
- return tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
106
 
107
- # Create Gradio interface
108
  iface = gr.Interface(
109
  fn=generate_text,
110
  inputs=[
 
1
  import gradio as gr
2
  import spaces
3
  import torch
4
+ from transformers import AutoTokenizer, pipeline
5
 
6
+ # Load the model and tokenizer
7
+ model_name = "akjindal53244/Llama-3.1-Storm-8B"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ pipe = pipeline(
10
+ "text-generation",
11
+ model=model_name,
12
+ torch_dtype=torch.bfloat16,
13
+ device_map="auto"
14
+ )
15
+
16
+ # HTML template
17
  HTML_TEMPLATE = """
18
  <style>
19
+ body {
20
+ background: linear-gradient(135deg, #f5f7fa, #c3cfe2);
21
+ font-family: Arial, sans-serif;
22
+ }
23
+ #app-header {
24
+ text-align: center;
25
+ background: rgba(255, 255, 255, 0.8);
26
+ padding: 20px;
27
+ border-radius: 10px;
28
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
29
+ position: relative;
30
+ max-width: 800px;
31
+ margin: 20px auto;
32
+ }
33
+ #app-header h1 {
34
+ color: #4A90E2;
35
+ font-size: 2em;
36
+ margin-bottom: 10px;
37
+ }
38
+ .llama-image {
39
+ position: relative;
40
+ transition: transform 0.3s;
41
+ display: inline-block;
42
+ margin-top: 20px;
43
+ }
44
+ .llama-image:hover {
45
+ transform: scale(1.05);
46
+ }
47
+ .llama-image img {
48
+ width: 200px;
49
+ border-radius: 10px;
50
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
51
+ }
52
+ .llama-description {
53
+ position: absolute;
54
+ bottom: -30px;
55
+ left: 50%;
56
+ transform: translateX(-50%);
57
+ background-color: #4A90E2;
58
+ color: white;
59
+ padding: 5px 10px;
60
+ border-radius: 5px;
61
+ opacity: 0;
62
+ transition: opacity 0.3s;
63
+ white-space: nowrap;
64
+ }
65
+ .llama-image:hover .llama-description {
66
+ opacity: 1;
67
+ }
68
+ .artifact {
69
+ position: absolute;
70
+ background: rgba(74, 144, 226, 0.1);
71
+ border-radius: 50%;
72
+ }
73
+ .artifact.large {
74
+ width: 300px;
75
+ height: 300px;
76
+ top: -50px;
77
+ left: -150px;
78
+ }
79
+ .artifact.medium {
80
+ width: 200px;
81
+ height: 200px;
82
+ bottom: -50px;
83
+ right: -100px;
84
+ }
85
+ .artifact.small {
86
+ width: 100px;
87
+ height: 100px;
88
+ top: 50%;
89
+ left: 50%;
90
+ transform: translate(-50%, -50%);
91
+ }
92
  </style>
93
+
94
  <div id="app-header">
95
  <div class="artifact large"></div>
96
  <div class="artifact medium"></div>
97
  <div class="artifact small"></div>
98
+
99
+ <h1>Llama-3.1-Storm-8B Text Generation</h1>
100
+ <p>Generate text using the powerful Llama-3.1-Storm-8B model. Enter a prompt and let the AI create!</p>
101
+
102
+ <div class="llama-image">
103
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/64c75c1237333ccfef30a602/tmOlbERGKP7JSODa6T06J.jpeg" alt="Llama">
104
+ <div class="llama-description">Llama-3.1-Storm-8B Model</div>
 
 
 
 
 
 
 
 
105
  </div>
106
  </div>
107
  """
108
 
 
 
 
 
 
 
 
 
 
109
  @spaces.GPU(duration=120)
110
  def generate_text(prompt, max_length, temperature):
111
  messages = [
 
114
  ]
115
  formatted_prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
116
 
117
+ outputs = pipe(
118
+ formatted_prompt,
 
 
119
  max_new_tokens=max_length,
120
  do_sample=True,
121
  temperature=temperature,
 
123
  top_p=0.95,
124
  )
125
 
126
+ return outputs[0]['generated_text'][len(formatted_prompt):]
127
 
 
128
  iface = gr.Interface(
129
  fn=generate_text,
130
  inputs=[