Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,7 @@ import gradio as gr
|
|
2 |
import spaces
|
3 |
import torch
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
-
|
6 |
-
import requests
|
7 |
-
from bs4 import BeautifulSoup
|
8 |
|
9 |
# Load the model and tokenizer
|
10 |
model_name = "akjindal53244/Llama-3.1-Storm-8B"
|
@@ -15,43 +13,14 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
15 |
device_map="auto"
|
16 |
)
|
17 |
|
18 |
-
def fetch_web_content(url):
|
19 |
-
try:
|
20 |
-
response = requests.get(url, timeout=10)
|
21 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
22 |
-
return ' '.join(p.get_text() for p in soup.find_all('p'))
|
23 |
-
except Exception as e:
|
24 |
-
print(f"Error fetching {url}: {str(e)}")
|
25 |
-
return "Could not fetch content from this URL"
|
26 |
-
|
27 |
-
def web_search(query, num_results=3):
|
28 |
-
try:
|
29 |
-
results = []
|
30 |
-
for j in search(query, num_results=num_results, advanced=True):
|
31 |
-
content = fetch_web_content(j.url)
|
32 |
-
results.append({
|
33 |
-
"title": j.title,
|
34 |
-
"url": j.url,
|
35 |
-
"content": content[:1000] # Limit content length
|
36 |
-
})
|
37 |
-
return results
|
38 |
-
except Exception as e:
|
39 |
-
print(f"Search error: {str(e)}")
|
40 |
-
return []
|
41 |
-
|
42 |
@spaces.GPU(duration=120)
|
43 |
-
def generate_text(prompt, max_length, temperature
|
44 |
-
if use_web:
|
45 |
-
search_results = web_search(prompt)
|
46 |
-
context = "\n".join([f"Source: {res['url']}\nContent: {res['content']}" for res in search_results])
|
47 |
-
prompt = f"Web Context:\n{context}\n\nUser Query: {prompt}"
|
48 |
-
|
49 |
messages = [
|
50 |
-
{"role": "system", "content": "You are a helpful assistant
|
51 |
{"role": "user", "content": prompt}
|
52 |
]
|
53 |
-
|
54 |
formatted_prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
|
|
|
55 |
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
|
56 |
|
57 |
outputs = model.generate(
|
@@ -65,135 +34,133 @@ def generate_text(prompt, max_length, temperature, use_web):
|
|
65 |
|
66 |
return tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
67 |
|
68 |
-
# CSS and UI components
|
69 |
-
css = """
|
70 |
-
:root {
|
71 |
-
--primary: #e94560;
|
72 |
-
--secondary: #1a1a2e;
|
73 |
-
--background: #16213e;
|
74 |
-
--text: #e0e0e0;
|
75 |
-
}
|
76 |
|
|
|
|
|
77 |
body {
|
78 |
-
background-color:
|
79 |
-
color:
|
80 |
-
font-family: '
|
81 |
}
|
82 |
-
|
83 |
.container {
|
84 |
-
max-width:
|
85 |
margin: auto;
|
86 |
padding: 20px;
|
87 |
}
|
88 |
-
|
89 |
.gradio-container {
|
90 |
-
background-color:
|
91 |
border-radius: 15px;
|
92 |
-
box-shadow: 0 4px
|
93 |
}
|
94 |
-
|
95 |
.header {
|
96 |
-
background:
|
97 |
-
padding:
|
98 |
border-radius: 15px 15px 0 0;
|
99 |
text-align: center;
|
100 |
-
margin-bottom:
|
101 |
}
|
102 |
-
|
103 |
.header h1 {
|
104 |
-
color:
|
105 |
-
font-size: 2.
|
106 |
-
margin-bottom:
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
}
|
109 |
-
|
110 |
.input-group, .output-group {
|
111 |
-
background-color:
|
112 |
-
padding:
|
113 |
-
border-radius:
|
114 |
-
margin-bottom:
|
115 |
-
|
|
|
|
|
|
|
116 |
}
|
117 |
-
|
118 |
.generate-btn {
|
119 |
-
background:
|
120 |
color: white !important;
|
121 |
-
border
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
}
|
124 |
-
|
125 |
.example-prompts ul {
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
}
|
129 |
"""
|
130 |
|
|
|
131 |
example_prompts = [
|
132 |
-
"
|
133 |
-
"
|
134 |
-
"
|
135 |
-
"
|
|
|
136 |
]
|
137 |
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
140 |
<div class="header">
|
141 |
-
<h1>Llama-3.1-Storm-8B
|
142 |
-
<p>
|
|
|
143 |
</div>
|
144 |
-
|
145 |
-
|
146 |
-
with gr.Tabs():
|
147 |
-
with gr.TabItem("Chat Assistant"):
|
148 |
-
with gr.Row():
|
149 |
-
with gr.Column(scale=3):
|
150 |
-
with gr.Group(elem_classes="example-prompts"):
|
151 |
-
gr.Markdown("## Example Queries")
|
152 |
-
example_btns = [gr.Button(prompt) for prompt in example_prompts]
|
153 |
-
|
154 |
-
with gr.Group(elem_classes="input-group"):
|
155 |
-
prompt = gr.Textbox(label="Your Query", placeholder="Enter your question...", lines=5)
|
156 |
-
|
157 |
-
with gr.Row():
|
158 |
-
web_search_toggle = gr.Checkbox(label="Enable Web Search", value=False)
|
159 |
-
num_results = gr.Slider(1, 5, value=3, step=1, label="Search Results")
|
160 |
-
|
161 |
-
with gr.Row():
|
162 |
-
max_length = gr.Slider(32, 1024, value=256, step=32, label="Response Length")
|
163 |
-
temperature = gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Creativity")
|
164 |
-
|
165 |
-
generate_btn = gr.Button("Generate Response", elem_classes="generate-btn")
|
166 |
-
|
167 |
-
with gr.Column(scale=2):
|
168 |
-
with gr.Group(elem_classes="output-group"):
|
169 |
-
output = gr.Textbox(label="Generated Response", lines=12)
|
170 |
-
with gr.Row():
|
171 |
-
copy_btn = gr.Button("Copy")
|
172 |
-
clear_btn = gr.Button("Clear")
|
173 |
-
|
174 |
-
with gr.TabItem("Web Results"):
|
175 |
-
web_results = gr.JSON(label="Search Results Preview")
|
176 |
-
|
177 |
-
# Event handlers
|
178 |
-
generate_btn.click(
|
179 |
-
generate_text,
|
180 |
-
inputs=[prompt, max_length, temperature, web_search_toggle],
|
181 |
-
outputs=output
|
182 |
-
).then(
|
183 |
-
lambda q: web_search(q) if q else [],
|
184 |
-
inputs=[prompt],
|
185 |
-
outputs=web_results
|
186 |
)
|
187 |
|
188 |
-
|
189 |
-
|
|
|
|
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
|
|
199 |
iface.launch()
|
|
|
2 |
import spaces
|
3 |
import torch
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
+
|
|
|
|
|
6 |
|
7 |
# Load the model and tokenizer
|
8 |
model_name = "akjindal53244/Llama-3.1-Storm-8B"
|
|
|
13 |
device_map="auto"
|
14 |
)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
@spaces.GPU(duration=120)
|
17 |
+
def generate_text(prompt, max_length, temperature):
|
|
|
|
|
|
|
|
|
|
|
18 |
messages = [
|
19 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
20 |
{"role": "user", "content": prompt}
|
21 |
]
|
|
|
22 |
formatted_prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
|
23 |
+
|
24 |
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
|
25 |
|
26 |
outputs = model.generate(
|
|
|
34 |
|
35 |
return tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
# Custom CSS
|
39 |
+
css = """
|
40 |
body {
|
41 |
+
background-color: #1a1a2e;
|
42 |
+
color: #e0e0e0;
|
43 |
+
font-family: 'Arial', sans-serif;
|
44 |
}
|
|
|
45 |
.container {
|
46 |
+
max-width: 900px;
|
47 |
margin: auto;
|
48 |
padding: 20px;
|
49 |
}
|
|
|
50 |
.gradio-container {
|
51 |
+
background-color: #16213e;
|
52 |
border-radius: 15px;
|
53 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
54 |
}
|
|
|
55 |
.header {
|
56 |
+
background-color: #0f3460;
|
57 |
+
padding: 20px;
|
58 |
border-radius: 15px 15px 0 0;
|
59 |
text-align: center;
|
60 |
+
margin-bottom: 20px;
|
61 |
}
|
|
|
62 |
.header h1 {
|
63 |
+
color: #e94560;
|
64 |
+
font-size: 2.5em;
|
65 |
+
margin-bottom: 10px;
|
66 |
+
}
|
67 |
+
.header p {
|
68 |
+
color: #a0a0a0;
|
69 |
+
}
|
70 |
+
.header img {
|
71 |
+
max-width: 300px;
|
72 |
+
border-radius: 10px;
|
73 |
+
margin: 15px auto;
|
74 |
+
display: block;
|
75 |
}
|
|
|
76 |
.input-group, .output-group {
|
77 |
+
background-color: #1a1a2e;
|
78 |
+
padding: 20px;
|
79 |
+
border-radius: 10px;
|
80 |
+
margin-bottom: 20px;
|
81 |
+
}
|
82 |
+
.input-group label, .output-group label {
|
83 |
+
color: #e94560;
|
84 |
+
font-weight: bold;
|
85 |
}
|
|
|
86 |
.generate-btn {
|
87 |
+
background-color: #e94560 !important;
|
88 |
color: white !important;
|
89 |
+
border: none !important;
|
90 |
+
border-radius: 5px !important;
|
91 |
+
padding: 10px 20px !important;
|
92 |
+
font-size: 16px !important;
|
93 |
+
cursor: pointer !important;
|
94 |
+
transition: background-color 0.3s ease !important;
|
95 |
+
}
|
96 |
+
.generate-btn:hover {
|
97 |
+
background-color: #c81e45 !important;
|
98 |
+
}
|
99 |
+
.example-prompts {
|
100 |
+
background-color: #1f2b47;
|
101 |
+
padding: 15px;
|
102 |
+
border-radius: 10px;
|
103 |
+
margin-bottom: 20px;
|
104 |
+
}
|
105 |
+
.example-prompts h3 {
|
106 |
+
color: #e94560;
|
107 |
+
margin-bottom: 10px;
|
108 |
}
|
|
|
109 |
.example-prompts ul {
|
110 |
+
list-style-type: none;
|
111 |
+
padding-left: 0;
|
112 |
+
}
|
113 |
+
.example-prompts li {
|
114 |
+
margin-bottom: 5px;
|
115 |
+
cursor: pointer;
|
116 |
+
transition: color 0.3s ease;
|
117 |
+
}
|
118 |
+
.example-prompts li:hover {
|
119 |
+
color: #e94560;
|
120 |
}
|
121 |
"""
|
122 |
|
123 |
+
# Example prompts
|
124 |
example_prompts = [
|
125 |
+
"Write a Python function to find the n-th Fibonacci number.",
|
126 |
+
"Explain the concept of recursion in programming.",
|
127 |
+
"What are the key differences between Python and JavaScript?",
|
128 |
+
"Tell me a short story about a time-traveling robot.",
|
129 |
+
"Describe the process of photosynthesis in simple terms."
|
130 |
]
|
131 |
|
132 |
+
# Gradio interface
|
133 |
+
# Gradio interface
|
134 |
+
with gr.Blocks(css=css) as iface:
|
135 |
+
gr.HTML(
|
136 |
+
"""
|
137 |
<div class="header">
|
138 |
+
<h1>Llama-3.1-Storm-8B Text Generation</h1>
|
139 |
+
<p>Generate text using the powerful Llama-3.1-Storm-8B model. Enter a prompt and let the AI create!</p>
|
140 |
+
<img src="https://cdn-uploads.huggingface.co/production/uploads/64c75c1237333ccfef30a602/tmOlbERGKP7JSODa6T06J.jpeg" alt="Llama">
|
141 |
</div>
|
142 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
)
|
144 |
|
145 |
+
with gr.Group():
|
146 |
+
with gr.Group(elem_classes="example-prompts"):
|
147 |
+
gr.HTML("<h3>Example Prompts:</h3>")
|
148 |
+
example_buttons = [gr.Button(prompt) for prompt in example_prompts]
|
149 |
|
150 |
+
with gr.Group(elem_classes="input-group"):
|
151 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=5)
|
152 |
+
max_length = gr.Slider(minimum=1, maximum=500, value=128, step=1, label="Max Length")
|
153 |
+
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
|
154 |
+
generate_btn = gr.Button("Generate", elem_classes="generate-btn")
|
155 |
+
|
156 |
+
with gr.Group(elem_classes="output-group"):
|
157 |
+
output = gr.Textbox(label="Generated Text", lines=10)
|
158 |
+
|
159 |
+
generate_btn.click(generate_text, inputs=[prompt, max_length, temperature], outputs=output)
|
160 |
+
|
161 |
+
# Set up example prompt buttons
|
162 |
+
for button in example_buttons:
|
163 |
+
button.click(lambda x: x, inputs=[button], outputs=[prompt])
|
164 |
|
165 |
+
# Launch the app
|
166 |
iface.launch()
|