Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,33 @@
|
|
1 |
-
import
|
2 |
-
import requests
|
3 |
-
import subprocess
|
4 |
-
import json
|
5 |
-
import time
|
6 |
-
import spaces
|
7 |
import gradio as gr
|
8 |
import random
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
# Placeholder greeting messages
|
37 |
GREETING_MESSAGES = [
|
38 |
"Greetings! I am AstroSage, your guide to the cosmos. What would you like to explore today?",
|
39 |
"Welcome to our cosmic journey! I am AstroSage. How may I assist you in understanding the universe?",
|
@@ -41,54 +35,80 @@ GREETING_MESSAGES = [
|
|
41 |
"The universe awaits! I'm AstroSage. What astronomical wonders shall we discuss?",
|
42 |
]
|
43 |
|
44 |
-
# Helper functions
|
45 |
-
def wait_until_llamacpp_ready():
|
46 |
-
"""Wait until the llama.cpp server is ready."""
|
47 |
-
trials = 0
|
48 |
-
while trials < LLAMA_CPP_SERVER_START_TIMEOUT:
|
49 |
-
try:
|
50 |
-
response = requests.get(LLAMA_CPP_SERVER_BASE + API_PATH_HEALTH)
|
51 |
-
if response.status_code == 200:
|
52 |
-
return
|
53 |
-
except requests.exceptions.RequestException:
|
54 |
-
pass
|
55 |
-
time.sleep(1)
|
56 |
-
trials += 1
|
57 |
-
raise TimeoutError("llama.cpp server did not start in time.")
|
58 |
-
|
59 |
-
def initial_greeting() -> History:
|
60 |
-
"""Generate the initial greeting from the assistant."""
|
61 |
-
return [{"role": "assistant", "content": random.choice(GREETING_MESSAGES)}]
|
62 |
|
63 |
-
def
|
64 |
-
"""
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
if history is None:
|
86 |
history = []
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
# Custom CSS for a space theme
|
94 |
custom_css = """
|
@@ -105,91 +125,74 @@ custom_css = """
|
|
105 |
}
|
106 |
"""
|
107 |
|
108 |
-
#
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
LLAMA_HOST="0.0.0.0",
|
114 |
-
LLAMA_PORT="8080",
|
115 |
-
LLAMA_ARG_CTX_SIZE=str(2048),
|
116 |
-
LLAMA_ARG_MODEL=MODEL_FILENAME,
|
117 |
-
LLAMA_ARG_N_GPU_LAYERS="9999",
|
118 |
-
))
|
119 |
-
|
120 |
-
try:
|
121 |
-
wait_until_llamacpp_ready()
|
122 |
-
|
123 |
-
# Create the Gradio interface
|
124 |
-
with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")) as demo:
|
125 |
-
gr.Markdown(
|
126 |
-
"""
|
127 |
-
# π AstroSage: Your Cosmic AI Companion
|
128 |
-
|
129 |
-
Welcome to AstroSage, an advanced AI assistant specializing in astronomy, astrophysics, and cosmology.
|
130 |
-
Powered by the AstroSage-Llama-3.1-8B model, I'm here to help you explore the wonders of the universe!
|
131 |
-
|
132 |
-
### What Can I Help You With?
|
133 |
-
- πͺ Explanations of astronomical phenomena
|
134 |
-
- π Space exploration and missions
|
135 |
-
- β Stars, galaxies, and cosmology
|
136 |
-
- π Planetary science and exoplanets
|
137 |
-
- π Astrophysics concepts and theories
|
138 |
-
- π Astronomical instruments and observations
|
139 |
-
|
140 |
-
Just type your question below and let's embark on a cosmic journey together!
|
141 |
-
"""
|
142 |
-
)
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
bubble_full_width=False,
|
147 |
-
show_label=True,
|
148 |
-
height=450,
|
149 |
-
type="messages"
|
150 |
-
)
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
[msg, chatbot],
|
178 |
-
queue=False
|
179 |
-
).then(
|
180 |
-
bot,
|
181 |
-
chatbot,
|
182 |
-
chatbot
|
183 |
)
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
|
191 |
-
|
|
|
192 |
demo.launch()
|
193 |
-
|
194 |
-
finally:
|
195 |
-
llama_proc.kill()
|
|
|
1 |
+
from threading import Thread
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
import random
|
4 |
+
import torch
|
5 |
+
from transformers import (
|
6 |
+
AutoModelForCausalLM,
|
7 |
+
AutoTokenizer,
|
8 |
+
AutoConfig,
|
9 |
+
TextIteratorStreamer
|
10 |
+
)
|
11 |
+
|
12 |
+
# Constants for the model and configuration
|
13 |
+
MODEL_ID = "universeTBD/astrollama"
|
14 |
+
WINDOW_SIZE = 4096
|
15 |
+
DEVICE = "cuda"
|
16 |
+
|
17 |
+
# Load model configuration, tokenizer, and model
|
18 |
+
config = AutoConfig.from_pretrained(pretrained_model_name_or_path=MODEL_ID)
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=MODEL_ID)
|
20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
21 |
+
pretrained_model_name_or_path=MODEL_ID,
|
22 |
+
config=config,
|
23 |
+
device_map="auto",
|
24 |
+
use_safetensors=True,
|
25 |
+
trust_remote_code=True,
|
26 |
+
load_in_4bit=True,
|
27 |
+
torch_dtype=torch.bfloat16
|
28 |
+
)
|
29 |
+
|
30 |
+
# Placeholder responses for when context is empty
|
|
|
31 |
GREETING_MESSAGES = [
|
32 |
"Greetings! I am AstroSage, your guide to the cosmos. What would you like to explore today?",
|
33 |
"Welcome to our cosmic journey! I am AstroSage. How may I assist you in understanding the universe?",
|
|
|
35 |
"The universe awaits! I'm AstroSage. What astronomical wonders shall we discuss?",
|
36 |
]
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
def generate_text(prompt: str, history: list, max_new_tokens=512, temperature=0.7, top_p=0.95, top_k=50):
|
40 |
+
"""
|
41 |
+
Generate a response using the transformer model.
|
42 |
+
"""
|
43 |
+
# Combine history into the prompt
|
44 |
+
formatted_history = "\n".join([f"{msg['role']}: {msg['content']}" for msg in history])
|
45 |
+
prompt_with_history = f"{formatted_history}\nUser: {prompt}\nAssistant:"
|
46 |
+
|
47 |
+
# Encode the prompt
|
48 |
+
inputs = tokenizer([prompt_with_history], return_tensors="pt", truncation=True).to(DEVICE)
|
49 |
+
input_length = inputs["input_ids"].shape[-1]
|
50 |
+
max_new_tokens = min(max_new_tokens, WINDOW_SIZE - input_length)
|
51 |
+
|
52 |
+
# Prepare text streamer for live updates
|
53 |
+
streamer = TextIteratorStreamer(
|
54 |
+
tokenizer=tokenizer,
|
55 |
+
timeout=10.0,
|
56 |
+
skip_prompt=True,
|
57 |
+
skip_special_tokens=True
|
58 |
+
)
|
59 |
+
generation_kwargs = dict(
|
60 |
+
**inputs,
|
61 |
+
streamer=streamer,
|
62 |
+
max_new_tokens=max_new_tokens,
|
63 |
+
do_sample=True,
|
64 |
+
top_p=top_p,
|
65 |
+
top_k=top_k,
|
66 |
+
temperature=temperature,
|
67 |
+
num_beams=1,
|
68 |
+
)
|
69 |
+
|
70 |
+
# Generate the response in a separate thread for streaming
|
71 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
72 |
+
thread.start()
|
73 |
+
|
74 |
+
# Collect and return the response
|
75 |
+
response = ""
|
76 |
+
for new_text in streamer:
|
77 |
+
response += new_text
|
78 |
+
yield response
|
79 |
+
|
80 |
+
|
81 |
+
def user(user_message, history):
|
82 |
+
"""
|
83 |
+
Add the user's message to the history.
|
84 |
+
"""
|
85 |
if history is None:
|
86 |
history = []
|
87 |
+
return "", history + [{"role": "user", "content": user_message}]
|
88 |
+
|
89 |
+
|
90 |
+
def bot(history):
|
91 |
+
"""
|
92 |
+
Generate the bot's response based on the history.
|
93 |
+
"""
|
94 |
+
if not history:
|
95 |
+
history = [{"role": "assistant", "content": random.choice(GREETING_MESSAGES)}]
|
96 |
+
last_user_message = history[-1]["content"] if history else ""
|
97 |
+
response_generator = generate_text(last_user_message, history)
|
98 |
+
history.append({"role": "assistant", "content": ""})
|
99 |
+
|
100 |
+
# Stream the response back
|
101 |
+
for partial_response in response_generator:
|
102 |
+
history[-1]["content"] = partial_response
|
103 |
+
yield history
|
104 |
+
|
105 |
+
|
106 |
+
def initial_greeting():
|
107 |
+
"""
|
108 |
+
Return the initial greeting message.
|
109 |
+
"""
|
110 |
+
return [{"role": "assistant", "content": random.choice(GREETING_MESSAGES)}]
|
111 |
+
|
112 |
|
113 |
# Custom CSS for a space theme
|
114 |
custom_css = """
|
|
|
125 |
}
|
126 |
"""
|
127 |
|
128 |
+
# Create the Gradio interface
|
129 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")) as demo:
|
130 |
+
gr.Markdown(
|
131 |
+
"""
|
132 |
+
# π AstroSage: Your Cosmic AI Companion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
+
Welcome to AstroSage, an advanced AI assistant specializing in astronomy, astrophysics, and cosmology.
|
135 |
+
Powered by the AstroSage-Llama-3.1-8B model, I'm here to help you explore the wonders of the universe!
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
+
### What Can I Help You With?
|
138 |
+
- πͺ Explanations of astronomical phenomena
|
139 |
+
- π Space exploration and missions
|
140 |
+
- β Stars, galaxies, and cosmology
|
141 |
+
- π Planetary science and exoplanets
|
142 |
+
- π Astrophysics concepts and theories
|
143 |
+
- π Astronomical instruments and observations
|
144 |
|
145 |
+
Just type your question below and let's embark on a cosmic journey together!
|
146 |
+
"""
|
147 |
+
)
|
148 |
+
|
149 |
+
chatbot = gr.Chatbot(
|
150 |
+
label="Chat with AstroSage",
|
151 |
+
bubble_full_width=False,
|
152 |
+
show_label=True,
|
153 |
+
height=450,
|
154 |
+
type="messages"
|
155 |
+
)
|
156 |
+
|
157 |
+
with gr.Row():
|
158 |
+
msg = gr.Textbox(
|
159 |
+
label="Type your message here",
|
160 |
+
placeholder="Ask me anything about space and astronomy...",
|
161 |
+
scale=9
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
)
|
163 |
+
clear = gr.Button("Clear Chat", scale=1)
|
164 |
+
|
165 |
+
# Example questions for quick start
|
166 |
+
gr.Examples(
|
167 |
+
examples=[
|
168 |
+
"What is a black hole and how does it form?",
|
169 |
+
"Can you explain the life cycle of a star?",
|
170 |
+
"What are exoplanets and how do we detect them?",
|
171 |
+
"Tell me about the James Webb Space Telescope.",
|
172 |
+
"What is dark matter and why is it important?"
|
173 |
+
],
|
174 |
+
inputs=msg,
|
175 |
+
label="Example Questions"
|
176 |
+
)
|
177 |
+
|
178 |
+
# Set up the message chain with streaming
|
179 |
+
msg.submit(
|
180 |
+
user,
|
181 |
+
[msg, chatbot],
|
182 |
+
[msg, chatbot],
|
183 |
+
queue=False
|
184 |
+
).then(
|
185 |
+
bot,
|
186 |
+
chatbot,
|
187 |
+
chatbot
|
188 |
+
)
|
189 |
+
|
190 |
+
# Clear button functionality
|
191 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
192 |
+
|
193 |
+
# Initial greeting
|
194 |
+
demo.load(initial_greeting, None, chatbot, queue=False)
|
195 |
|
196 |
+
# Launch the app
|
197 |
+
if __name__ == "__main__":
|
198 |
demo.launch()
|
|
|
|
|
|