Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,34 @@ from llama_cpp import Llama
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
import random
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def user(user_message, history):
|
| 8 |
"""Add user message to chat history."""
|
| 9 |
if history is None:
|
|
@@ -50,120 +78,91 @@ def bot(history):
|
|
| 50 |
|
| 51 |
def initial_greeting():
|
| 52 |
"""Return properly formatted initial greeting."""
|
| 53 |
-
|
| 54 |
-
"Greetings! I am AstroSage, your guide to the cosmos. What would you like to explore today?",
|
| 55 |
-
"Welcome to our cosmic journey! I am AstroSage. How may I assist you in understanding the universe?",
|
| 56 |
-
"AstroSage here. Ready to explore the mysteries of space and time. How may I be of assistance?",
|
| 57 |
-
"The universe awaits! I'm AstroSage. What astronomical wonders shall we discuss?",
|
| 58 |
-
]
|
| 59 |
-
return [{"role": "assistant", "content": random.choice(greeting_messages)}]
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
@spaces.GPU
|
| 63 |
-
def main():
|
| 64 |
-
# Initialize model
|
| 65 |
-
model_path = hf_hub_download(
|
| 66 |
-
repo_id="AstroMLab/AstroSage-8B-GGUF",
|
| 67 |
-
filename="AstroSage-8B-Q8_0.gguf"
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
llm = Llama(
|
| 71 |
-
model_path=model_path,
|
| 72 |
-
n_ctx=2048,
|
| 73 |
-
n_threads=8,
|
| 74 |
-
chat_format="llama-3",
|
| 75 |
-
seed=42,
|
| 76 |
-
f16_kv=True,
|
| 77 |
-
logits_all=False,
|
| 78 |
-
use_mmap=True,
|
| 79 |
-
use_gpu=True,
|
| 80 |
-
)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
Welcome to AstroSage, an advanced AI assistant specializing in astronomy, astrophysics, and cosmology.
|
| 104 |
-
Powered by the AstroSage-Llama-3.1-8B model, I'm here to help you explore the wonders of the universe!
|
| 105 |
-
|
| 106 |
-
### What Can I Help You With?
|
| 107 |
-
- πͺ Explanations of astronomical phenomena
|
| 108 |
-
- π Space exploration and missions
|
| 109 |
-
- β Stars, galaxies, and cosmology
|
| 110 |
-
- π Planetary science and exoplanets
|
| 111 |
-
- π Astrophysics concepts and theories
|
| 112 |
-
- π Astronomical instruments and observations
|
| 113 |
-
|
| 114 |
-
Just type your question below and let's embark on a cosmic journey together!
|
| 115 |
-
"""
|
| 116 |
-
)
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
bubble_full_width=False,
|
| 121 |
-
show_label=True,
|
| 122 |
-
height=450,
|
| 123 |
-
type="messages"
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
with gr.Row():
|
| 127 |
-
msg = gr.Textbox(
|
| 128 |
-
label="Type your message here",
|
| 129 |
-
placeholder="Ask me anything about space and astronomy...",
|
| 130 |
-
scale=9
|
| 131 |
-
)
|
| 132 |
-
clear = gr.Button("Clear Chat", scale=1)
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
"What is dark matter and why is it important?"
|
| 142 |
-
],
|
| 143 |
-
inputs=msg,
|
| 144 |
-
label="Example Questions"
|
| 145 |
-
)
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
)
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
# Launch the app
|
| 168 |
if __name__ == "__main__":
|
| 169 |
-
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
import random
|
| 6 |
|
| 7 |
+
# Initialize model
|
| 8 |
+
model_path = hf_hub_download(
|
| 9 |
+
repo_id="AstroMLab/AstroSage-8B-GGUF",
|
| 10 |
+
filename="AstroSage-8B-Q8_0.gguf"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
llm = Llama(
|
| 14 |
+
model_path=model_path,
|
| 15 |
+
n_ctx=2048,
|
| 16 |
+
n_threads=8,
|
| 17 |
+
chat_format="llama-3",
|
| 18 |
+
seed=42,
|
| 19 |
+
f16_kv=True,
|
| 20 |
+
logits_all=False,
|
| 21 |
+
use_mmap=True,
|
| 22 |
+
use_gpu=True,
|
| 23 |
+
n_gpu_layers=-1, # to ensure all layers are on GPU
|
| 24 |
+
offload_kqv=True # for better memory management
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Placeholder responses for when context is empty
|
| 28 |
+
GREETING_MESSAGES = [
|
| 29 |
+
"Greetings! I am AstroSage, your guide to the cosmos. What would you like to explore today?",
|
| 30 |
+
"Welcome to our cosmic journey! I am AstroSage. How may I assist you in understanding the universe?",
|
| 31 |
+
"AstroSage here. Ready to explore the mysteries of space and time. How may I be of assistance?",
|
| 32 |
+
"The universe awaits! I'm AstroSage. What astronomical wonders shall we discuss?",
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
def user(user_message, history):
|
| 36 |
"""Add user message to chat history."""
|
| 37 |
if history is None:
|
|
|
|
| 78 |
|
| 79 |
def initial_greeting():
|
| 80 |
"""Return properly formatted initial greeting."""
|
| 81 |
+
return [{"role": "assistant", "content": random.choice(GREETING_MESSAGES)}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# Custom CSS for a space theme
|
| 84 |
+
custom_css = """
|
| 85 |
+
#component-0 {
|
| 86 |
+
background-color: #1a1a2e;
|
| 87 |
+
border-radius: 15px;
|
| 88 |
+
padding: 20px;
|
| 89 |
+
}
|
| 90 |
+
.dark {
|
| 91 |
+
background-color: #0f0f1a;
|
| 92 |
+
}
|
| 93 |
+
.contain {
|
| 94 |
+
max-width: 1200px !important;
|
| 95 |
+
}
|
| 96 |
+
"""
|
| 97 |
|
| 98 |
+
# Create the Gradio interface
|
| 99 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")) as demo:
|
| 100 |
+
gr.Markdown(
|
| 101 |
+
"""
|
| 102 |
+
# π AstroSage: Your Cosmic AI Companion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
Welcome to AstroSage, an advanced AI assistant specializing in astronomy, astrophysics, and cosmology.
|
| 105 |
+
Powered by the AstroSage-Llama-3.1-8B model, I'm here to help you explore the wonders of the universe!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
### What Can I Help You With?
|
| 108 |
+
- πͺ Explanations of astronomical phenomena
|
| 109 |
+
- π Space exploration and missions
|
| 110 |
+
- β Stars, galaxies, and cosmology
|
| 111 |
+
- π Planetary science and exoplanets
|
| 112 |
+
- π Astrophysics concepts and theories
|
| 113 |
+
- π Astronomical instruments and observations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
Just type your question below and let's embark on a cosmic journey together!
|
| 116 |
+
"""
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
chatbot = gr.Chatbot(
|
| 120 |
+
label="Chat with AstroSage",
|
| 121 |
+
bubble_full_width=False,
|
| 122 |
+
show_label=True,
|
| 123 |
+
height=450,
|
| 124 |
+
type="messages"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
with gr.Row():
|
| 128 |
+
msg = gr.Textbox(
|
| 129 |
+
label="Type your message here",
|
| 130 |
+
placeholder="Ask me anything about space and astronomy...",
|
| 131 |
+
scale=9
|
| 132 |
)
|
| 133 |
+
clear = gr.Button("Clear Chat", scale=1)
|
| 134 |
+
|
| 135 |
+
# Example questions for quick start
|
| 136 |
+
gr.Examples(
|
| 137 |
+
examples=[
|
| 138 |
+
"What is a black hole and how does it form?",
|
| 139 |
+
"Can you explain the life cycle of a star?",
|
| 140 |
+
"What are exoplanets and how do we detect them?",
|
| 141 |
+
"Tell me about the James Webb Space Telescope.",
|
| 142 |
+
"What is dark matter and why is it important?"
|
| 143 |
+
],
|
| 144 |
+
inputs=msg,
|
| 145 |
+
label="Example Questions"
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# Set up the message chain with streaming
|
| 149 |
+
msg.submit(
|
| 150 |
+
user,
|
| 151 |
+
[msg, chatbot],
|
| 152 |
+
[msg, chatbot],
|
| 153 |
+
queue=False
|
| 154 |
+
).then(
|
| 155 |
+
bot,
|
| 156 |
+
chatbot,
|
| 157 |
+
chatbot
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
# Clear button functionality
|
| 161 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 162 |
+
|
| 163 |
+
# Initial greeting
|
| 164 |
+
demo.load(initial_greeting, None, chatbot, queue=False)
|
| 165 |
|
| 166 |
# Launch the app
|
| 167 |
if __name__ == "__main__":
|
| 168 |
+
demo.launch()
|