Tijmen2 commited on
Commit
5a18dfb
Β·
verified Β·
1 Parent(s): 6288ea1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +164 -161
app.py CHANGED
@@ -1,39 +1,33 @@
1
- import os
2
- import requests
3
- import subprocess
4
- import json
5
- import time
6
- import spaces
7
  import gradio as gr
8
  import random
9
- from typing import List, Optional, Tuple, Dict
10
-
11
- # Constants for the llama.cpp server
12
- API_PATH_HEALTH = "/health"
13
- API_PATH_COMPLETIONS = "/chat/completions"
14
- LLAMA_CPP_SERVER_BASE = "http://127.0.0.1:8080"
15
- LLAMA_CPP_SERVER_START_TIMEOUT = 50 # seconds
16
- MODEL_FILENAME = "AstroSage-8B-Q8_0.gguf"
17
- HF_MODEL_ID = "AstroMLab/AstroSage-8B-GGUF"
18
-
19
- # Ensure the model is available
20
- if not os.path.exists(MODEL_FILENAME):
21
- url = f"https://huggingface.co/{HF_MODEL_ID}/resolve/main/{MODEL_FILENAME}"
22
- subprocess.check_call(["curl", "-o", MODEL_FILENAME, "-L", url])
23
-
24
- if not os.path.exists("llama-server"):
25
- subprocess.check_call("curl -o llama-server -L https://ngxson-llamacpp-builder.hf.space/llama-server", shell=True)
26
- subprocess.check_call("chmod +x llama-server", shell=True)
27
-
28
- # Roles and History Types
29
- class Role:
30
- SYSTEM = "system"
31
- USER = "user"
32
- ASSISTANT = "assistant"
33
-
34
- History = List[Dict[str, str]] # Chat history with "role" and "content"
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 send_request_to_llama(query: str, history: History) -> str:
64
- """Send a chat request to the llama.cpp server."""
65
- messages = [{"role": Role.SYSTEM, "content": "You are AstroSage, an AI assistant specializing in astronomy, astrophysics, and cosmology."}]
66
- messages.extend(history)
67
- messages.append({"role": Role.USER, "content": query})
68
-
69
- headers = {"Content-Type": "application/json"}
70
- data = {"temperature": 0.7, "messages": messages, "stream": True}
71
- response = requests.post(LLAMA_CPP_SERVER_BASE + API_PATH_COMPLETIONS, headers=headers, json=data, stream=True)
72
- response.raise_for_status()
73
-
74
- response_text = ""
75
- for line in response.iter_lines():
76
- line = line.decode("utf-8")
77
- if line.startswith("data: ") and not line.endswith("[DONE]"):
78
- data = json.loads(line[len("data: "):])
79
- response_text += data["choices"][0]["delta"].get("content", "")
80
- return response_text
81
-
82
- @spaces.GPU
83
- def bot(history: Optional[History]) -> History:
84
- """Generate the assistant's response."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  if history is None:
86
  history = []
87
- query = history[-1]["content"]
88
-
89
- response = send_request_to_llama(query, history[:-1])
90
- history.append({"role": "assistant", "content": response})
91
- return history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  # Custom CSS for a space theme
94
  custom_css = """
@@ -105,91 +125,74 @@ custom_css = """
105
  }
106
  """
107
 
108
- # Launch llama.cpp server
109
- llama_proc = subprocess.Popen([
110
- "./llama-server"
111
- ], env=dict(
112
- os.environ,
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
- chatbot = gr.Chatbot(
145
- label="Chat with AstroSage",
146
- bubble_full_width=False,
147
- show_label=True,
148
- height=450,
149
- type="messages"
150
- )
151
 
152
- with gr.Row():
153
- msg = gr.Textbox(
154
- label="Type your message here",
155
- placeholder="Ask me anything about space and astronomy...",
156
- scale=9
157
- )
158
- clear = gr.Button("Clear Chat", scale=1)
159
 
160
- # Example questions for quick start
161
- gr.Examples(
162
- examples=[
163
- "What is a black hole and how does it form?",
164
- "Can you explain the life cycle of a star?",
165
- "What are exoplanets and how do we detect them?",
166
- "Tell me about the James Webb Space Telescope.",
167
- "What is dark matter and why is it important?"
168
- ],
169
- inputs=msg,
170
- label="Example Questions"
171
- )
172
-
173
- # Set up the message chain
174
- msg.submit(
175
- lambda x, y: (x, y + [{"role": "user", "content": x}]),
176
- [msg, chatbot],
177
- [msg, chatbot],
178
- queue=False
179
- ).then(
180
- bot,
181
- chatbot,
182
- chatbot
183
  )
184
-
185
- # Clear button functionality
186
- clear.click(lambda: None, None, chatbot, queue=False)
187
-
188
- # Initial greeting
189
- demo.load(initial_greeting, None, chatbot, queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
 
191
- # Launch the app
 
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()