Tijmen2 commited on
Commit
c3386a0
Β·
verified Β·
1 Parent(s): 808fb76

Update app.py

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