Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
from llama_cpp import Llama
|
3 |
from huggingface_hub import hf_hub_download
|
|
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
model_path = hf_hub_download(
|
6 |
repo_id="AstroMLab/AstroSage-8B-GGUF",
|
7 |
filename="AstroSage-8B-Q8_0.gguf"
|
@@ -11,7 +47,7 @@ llm = Llama(
|
|
11 |
model_path=model_path,
|
12 |
n_ctx=2048,
|
13 |
n_threads=4,
|
14 |
-
chat_format="llama-
|
15 |
seed=42,
|
16 |
f16_kv=True,
|
17 |
logits_all=False,
|
@@ -19,6 +55,17 @@ llm = Llama(
|
|
19 |
use_gpu=True
|
20 |
)
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
23 |
messages = [{"role": "system", "content": system_message}]
|
24 |
for user_msg, assistant_msg in history:
|
@@ -35,18 +82,124 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
35 |
top_p=top_p
|
36 |
)
|
37 |
|
38 |
-
# Extract the assistant's message from the response
|
39 |
return response["choices"][0]["message"]["content"]
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
if __name__ == "__main__":
|
52 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from llama_cpp import Llama
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
+
import random
|
5 |
|
6 |
+
# Custom CSS for better styling
|
7 |
+
custom_css = """
|
8 |
+
.gradio-container {
|
9 |
+
background: linear-gradient(to bottom, #1a1a2e, #16213e) !important;
|
10 |
+
}
|
11 |
+
.header-text {
|
12 |
+
text-align: center;
|
13 |
+
color: #e2e8f0;
|
14 |
+
font-size: 2.5em;
|
15 |
+
font-weight: bold;
|
16 |
+
margin: 1em 0;
|
17 |
+
text-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
|
18 |
+
}
|
19 |
+
.subheader {
|
20 |
+
text-align: center;
|
21 |
+
color: #94a3b8;
|
22 |
+
font-size: 1.2em;
|
23 |
+
margin-bottom: 2em;
|
24 |
+
}
|
25 |
+
.controls-section {
|
26 |
+
background: rgba(255, 255, 255, 0.05);
|
27 |
+
padding: 1.5em;
|
28 |
+
border-radius: 10px;
|
29 |
+
margin: 1em 0;
|
30 |
+
}
|
31 |
+
.model-info {
|
32 |
+
background: rgba(0, 0, 0, 0.2);
|
33 |
+
padding: 1em;
|
34 |
+
border-radius: 8px;
|
35 |
+
margin-top: 1em;
|
36 |
+
color: #94a3b8;
|
37 |
+
}
|
38 |
+
"""
|
39 |
+
|
40 |
+
# Initialize model
|
41 |
model_path = hf_hub_download(
|
42 |
repo_id="AstroMLab/AstroSage-8B-GGUF",
|
43 |
filename="AstroSage-8B-Q8_0.gguf"
|
|
|
47 |
model_path=model_path,
|
48 |
n_ctx=2048,
|
49 |
n_threads=4,
|
50 |
+
chat_format="llama-3",
|
51 |
seed=42,
|
52 |
f16_kv=True,
|
53 |
logits_all=False,
|
|
|
55 |
use_gpu=True
|
56 |
)
|
57 |
|
58 |
+
# Placeholder responses for when context is empty
|
59 |
+
GREETING_MESSAGES = [
|
60 |
+
"Greetings! I am AstroSage, your guide to the cosmos. What would you like to explore today?",
|
61 |
+
"Welcome to our cosmic journey! I am AstroSage. How may I assist you in understanding the universe?",
|
62 |
+
"AstroSage here. Ready to explore the mysteries of space and time. How may I be of assistance?",
|
63 |
+
"The universe awaits! I'm AstroSage. What astronomical wonders shall we discuss?",
|
64 |
+
]
|
65 |
+
|
66 |
+
def get_random_greeting():
|
67 |
+
return random.choice(GREETING_MESSAGES)
|
68 |
+
|
69 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
70 |
messages = [{"role": "system", "content": system_message}]
|
71 |
for user_msg, assistant_msg in history:
|
|
|
82 |
top_p=top_p
|
83 |
)
|
84 |
|
|
|
85 |
return response["choices"][0]["message"]["content"]
|
86 |
|
87 |
+
def regenerate(message, history, system_message, max_tokens, temperature, top_p):
|
88 |
+
# Remove the last assistant message from history
|
89 |
+
if history and len(history) > 0:
|
90 |
+
history = history[:-1]
|
91 |
+
|
92 |
+
# Generate a new response
|
93 |
+
return respond(message, history, system_message, max_tokens, temperature, top_p)
|
94 |
+
|
95 |
+
def clear_context():
|
96 |
+
return [], get_random_greeting()
|
97 |
+
|
98 |
+
with gr.Blocks(css=custom_css) as demo:
|
99 |
+
gr.HTML(
|
100 |
+
"""
|
101 |
+
<div class="header-text">π AstroSage-LLAMA-3.1-8B</div>
|
102 |
+
<div class="subheader">Your AI Guide to the Cosmos</div>
|
103 |
+
"""
|
104 |
+
)
|
105 |
+
|
106 |
+
chatbot = gr.Chatbot(
|
107 |
+
value=[[None, get_random_greeting()]],
|
108 |
+
height=400,
|
109 |
+
show_label=False,
|
110 |
+
)
|
111 |
+
msg = gr.Textbox(
|
112 |
+
placeholder="Ask about astronomy, astrophysics, or cosmology...",
|
113 |
+
show_label=False,
|
114 |
+
)
|
115 |
+
|
116 |
+
with gr.Accordion("Advanced Settings", open=False) as advanced_settings:
|
117 |
+
system_msg = gr.Textbox(
|
118 |
+
value="You are AstroSage, a highly knowledgeable AI assistant specialized in astronomy, astrophysics, and cosmology. Provide accurate, engaging, and educational responses about space science and the universe.",
|
119 |
+
label="System Message",
|
120 |
+
lines=3
|
121 |
+
)
|
122 |
+
with gr.Row():
|
123 |
+
max_tokens = gr.Slider(
|
124 |
+
minimum=1,
|
125 |
+
maximum=2048,
|
126 |
+
value=512,
|
127 |
+
step=1,
|
128 |
+
label="Max Tokens"
|
129 |
+
)
|
130 |
+
temperature = gr.Slider(
|
131 |
+
minimum=0.1,
|
132 |
+
maximum=4.0,
|
133 |
+
value=0.7,
|
134 |
+
step=0.1,
|
135 |
+
label="Temperature"
|
136 |
+
)
|
137 |
+
top_p = gr.Slider(
|
138 |
+
minimum=0.1,
|
139 |
+
maximum=1.0,
|
140 |
+
value=0.9,
|
141 |
+
step=0.05,
|
142 |
+
label="Top-p"
|
143 |
+
)
|
144 |
+
|
145 |
+
with gr.Row():
|
146 |
+
clear = gr.Button("π New Chat")
|
147 |
+
regenerate_btn = gr.Button("π Regenerate")
|
148 |
+
submit = gr.Button("Send π", variant="primary")
|
149 |
+
|
150 |
+
gr.HTML(
|
151 |
+
"""
|
152 |
+
<div class="model-info">
|
153 |
+
<p>π Model: AstroSage-LLAMA-3.1-8B (8-bit Quantized)</p>
|
154 |
+
<p>π§ Built with llama.cpp, Gradio, and Python</p>
|
155 |
+
<p>π« Specialized in astronomy, astrophysics, and cosmology</p>
|
156 |
+
</div>
|
157 |
+
"""
|
158 |
+
)
|
159 |
+
|
160 |
+
# Set up event handlers
|
161 |
+
msg.submit(
|
162 |
+
respond,
|
163 |
+
[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
164 |
+
[chatbot],
|
165 |
+
queue=False
|
166 |
+
).then(
|
167 |
+
lambda: "",
|
168 |
+
None,
|
169 |
+
[msg],
|
170 |
+
queue=False
|
171 |
+
)
|
172 |
+
|
173 |
+
submit.click(
|
174 |
+
respond,
|
175 |
+
[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
176 |
+
[chatbot],
|
177 |
+
queue=False
|
178 |
+
).then(
|
179 |
+
lambda: "",
|
180 |
+
None,
|
181 |
+
[msg],
|
182 |
+
queue=False
|
183 |
+
)
|
184 |
+
|
185 |
+
regenerate_btn.click(
|
186 |
+
regenerate,
|
187 |
+
[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
188 |
+
[chatbot],
|
189 |
+
queue=False
|
190 |
+
)
|
191 |
+
|
192 |
+
clear.click(
|
193 |
+
clear_context,
|
194 |
+
None,
|
195 |
+
[chatbot, msg],
|
196 |
+
queue=False
|
197 |
+
)
|
198 |
|
199 |
if __name__ == "__main__":
|
200 |
+
demo.launch(
|
201 |
+
share=False,
|
202 |
+
debug=True,
|
203 |
+
server_name="0.0.0.0",
|
204 |
+
server_port=7860,
|
205 |
+
)
|