AstroSage / app.py
Tijmen2's picture
Update app.py
a65b868 verified
raw
history blame
5.32 kB
import spaces
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
import torch
import random
# Define model parameters for 8-bit quantized loading
model_name = "AstroMLab/AstroSage-8B"
# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Load the model with 8-bit quantization using bitsandbytes
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
load_in_8bit=True, # Enable 8-bit quantization
device_map="auto" # Automatically assign layers to available GPUs
)
streamer = TextStreamer(tokenizer)
# Placeholder responses for when context is empty
GREETING_MESSAGES = [
"Greetings! I am AstroSage, your guide to the cosmos. What would you like to explore today?",
"Welcome to our cosmic journey! I am AstroSage. How may I assist you in understanding the universe?",
"AstroSage here. Ready to explore the mysteries of space and time. How may I be of assistance?",
"The universe awaits! I'm AstroSage. What astronomical wonders shall we discuss?",
]
def user(user_message, history):
"""Add user message to chat history."""
if history is None:
history = []
return "", history + [{"role": "user", "content": user_message}]
@spaces.GPU(duration=20)
def bot(history):
"""Generate the chatbot response."""
if not history:
history = []
# Prepare input prompt for the model
system_prompt = (
"You are AstroSage, an intelligent AI assistant specializing in astronomy, astrophysics, and cosmology. "
"Provide accurate, scientific information while making complex concepts accessible. "
"You're enthusiastic about space exploration and maintain a sense of wonder about the cosmos."
)
# Construct the chat history as a single input string
prompt = system_prompt + "\n\n"
for message in history:
if message["role"] == "user":
prompt += f"User: {message['content']}\n"
else:
prompt += f"AstroSage: {message['content']}\n"
prompt += "AstroSage: "
# Generate response
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=512,
temperature=0.7,
top_p=0.95,
do_sample=True,
streamer=streamer
)
# Decode the generated output and update history
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
response_text = response_text[len(prompt):].strip()
history.append({"role": "assistant", "content": response_text})
yield history
def initial_greeting():
"""Return properly formatted initial greeting."""
return [{"role": "assistant", "content": random.choice(GREETING_MESSAGES)}]
# Custom CSS for a space theme
custom_css = """
#component-0 {
background-color: #1a1a2e;
border-radius: 15px;
padding: 20px;
}
.dark {
background-color: #0f0f1a;
}
.contain {
max-width: 1200px !important;
}
"""
# Create the Gradio interface
with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")) as demo:
gr.Markdown(
"""
# 🌌 AstroSage: Your Cosmic AI Companion
Welcome to AstroSage, an advanced AI assistant specializing in astronomy, astrophysics, and cosmology.
Powered by the AstroSage-Llama-3.1-8B model, I'm here to help you explore the wonders of the universe!
### What Can I Help You With?
- πŸͺ Explanations of astronomical phenomena
- πŸš€ Space exploration and missions
- ⭐ Stars, galaxies, and cosmology
- 🌍 Planetary science and exoplanets
- πŸ“Š Astrophysics concepts and theories
- πŸ”­ Astronomical instruments and observations
Just type your question below and let's embark on a cosmic journey together!
"""
)
chatbot = gr.Chatbot(
label="Chat with AstroSage",
bubble_full_width=False,
show_label=True,
height=450,
type="messages"
)
with gr.Row():
msg = gr.Textbox(
label="Type your message here",
placeholder="Ask me anything about space and astronomy...",
scale=9
)
clear = gr.Button("Clear Chat", scale=1)
# Example questions for quick start
gr.Examples(
examples=[
"What is a black hole and how does it form?",
"Can you explain the life cycle of a star?",
"What are exoplanets and how do we detect them?",
"Tell me about the James Webb Space Telescope.",
"What is dark matter and why is it important?"
],
inputs=msg,
label="Example Questions"
)
# Set up the message chain with streaming
msg.submit(
user,
[msg, chatbot],
[msg, chatbot],
queue=False
).then(
bot,
chatbot,
chatbot
)
# Clear button functionality
clear.click(lambda: None, None, chatbot, queue=False)
# Initial greeting
demo.load(initial_greeting, None, chatbot, queue=False)
# Launch the app
if __name__ == "__main__":
demo.launch()