Update app.py
Browse files
app.py
CHANGED
@@ -29,46 +29,127 @@ GREETING_MESSAGES = [
|
|
29 |
"The universe awaits! I'm AstroSage. What astronomical wonders shall we discuss?",
|
30 |
]
|
31 |
|
32 |
-
def
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
messages = [{"role": "system", "content": system_message}]
|
38 |
-
for user, assistant in history:
|
39 |
-
messages.append({"role": "user", "content": user})
|
40 |
if assistant:
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
with gr.Row():
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
-
|
|
|
|
|
|
29 |
"The universe awaits! I'm AstroSage. What astronomical wonders shall we discuss?",
|
30 |
]
|
31 |
|
32 |
+
def format_chat_history(history):
|
33 |
+
"""Format the chat history for the model."""
|
34 |
+
formatted_messages = []
|
35 |
+
for human, assistant in history:
|
36 |
+
formatted_messages.append({"role": "user", "content": human})
|
|
|
|
|
|
|
37 |
if assistant:
|
38 |
+
formatted_messages.append({"role": "assistant", "content": assistant})
|
39 |
+
return formatted_messages
|
40 |
|
41 |
+
def generate_response(message, history):
|
42 |
+
"""Generate response using the LLM."""
|
43 |
+
if not message:
|
44 |
+
return random.choice(GREETING_MESSAGES)
|
45 |
+
|
46 |
+
formatted_history = format_chat_history(history)
|
47 |
+
|
48 |
+
response = llm.create_chat_completion(
|
49 |
+
messages=[
|
50 |
+
{"role": "system", "content": "You are AstroSage, an intelligent AI assistant specializing in astronomy, astrophysics, and space science. You provide accurate, scientific information while making complex concepts accessible. You're enthusiastic about space exploration and maintain a sense of wonder about the cosmos."},
|
51 |
+
*formatted_history,
|
52 |
+
{"role": "user", "content": message}
|
53 |
+
],
|
54 |
+
max_tokens=512,
|
55 |
+
temperature=0.7,
|
56 |
+
top_p=0.95,
|
57 |
+
stream=True
|
58 |
+
)
|
59 |
+
|
60 |
+
partial_message = ""
|
61 |
+
for chunk in response:
|
62 |
+
if chunk and "content" in chunk["choices"][0]["delta"]:
|
63 |
+
partial_message += chunk["choices"][0]["delta"]["content"]
|
64 |
+
yield partial_message
|
65 |
|
66 |
+
# Custom CSS for a space theme
|
67 |
+
custom_css = """
|
68 |
+
#component-0 {
|
69 |
+
background-color: #1a1a2e;
|
70 |
+
border-radius: 15px;
|
71 |
+
padding: 20px;
|
72 |
+
}
|
73 |
+
.dark {
|
74 |
+
background-color: #0f0f1a;
|
75 |
+
}
|
76 |
+
.contain {
|
77 |
+
max-width: 1200px !important;
|
78 |
+
}
|
79 |
+
"""
|
80 |
|
81 |
+
# Create the Gradio interface
|
82 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")) as demo:
|
83 |
+
gr.Markdown(
|
84 |
+
"""
|
85 |
+
# π AstroSage: Your Cosmic AI Companion
|
86 |
+
|
87 |
+
Welcome to AstroSage, an advanced AI assistant specializing in astronomy, astrophysics, and space science.
|
88 |
+
Powered by the AstroSage-8B model, I'm here to help you explore the wonders of the universe!
|
89 |
+
|
90 |
+
### What Can I Help You With?
|
91 |
+
- πͺ Explanations of astronomical phenomena
|
92 |
+
- π Space exploration and missions
|
93 |
+
- β Stars, galaxies, and cosmic objects
|
94 |
+
- π Planetary science and exoplanets
|
95 |
+
- π Astrophysics concepts and theories
|
96 |
+
- π Astronomical instruments and observations
|
97 |
+
|
98 |
+
Just type your question below and let's embark on a cosmic journey together!
|
99 |
+
"""
|
100 |
+
)
|
101 |
+
|
102 |
+
chatbot = gr.Chatbot(
|
103 |
+
label="Chat with AstroSage",
|
104 |
+
bubble_full_width=False,
|
105 |
+
show_label=True,
|
106 |
+
height=450
|
107 |
+
)
|
108 |
+
|
109 |
with gr.Row():
|
110 |
+
msg = gr.Textbox(
|
111 |
+
label="Type your message here",
|
112 |
+
placeholder="Ask me anything about space and astronomy...",
|
113 |
+
scale=9
|
114 |
+
)
|
115 |
+
clear = gr.Button("Clear Chat", scale=1)
|
116 |
+
|
117 |
+
# Example questions for quick start
|
118 |
+
gr.Examples(
|
119 |
+
examples=[
|
120 |
+
"What is a black hole and how does it form?",
|
121 |
+
"Can you explain the life cycle of a star?",
|
122 |
+
"What are exoplanets and how do we detect them?",
|
123 |
+
"Tell me about the James Webb Space Telescope.",
|
124 |
+
"What is dark matter and why is it important?"
|
125 |
+
],
|
126 |
+
inputs=msg,
|
127 |
+
label="Example Questions"
|
128 |
+
)
|
129 |
+
|
130 |
+
# Event handlers
|
131 |
+
msg.submit(
|
132 |
+
generate_response,
|
133 |
+
[msg, chatbot],
|
134 |
+
[msg, chatbot],
|
135 |
+
queue=True
|
136 |
+
)
|
137 |
+
|
138 |
+
clear.click(
|
139 |
+
lambda: (None, None),
|
140 |
+
None,
|
141 |
+
[msg, chatbot],
|
142 |
+
queue=False
|
143 |
+
)
|
144 |
+
|
145 |
+
# Initial greeting
|
146 |
+
demo.load(
|
147 |
+
lambda: (random.choice(GREETING_MESSAGES), []),
|
148 |
+
None,
|
149 |
+
[msg, chatbot],
|
150 |
+
queue=False
|
151 |
+
)
|
152 |
|
153 |
+
# Launch the app
|
154 |
+
if __name__ == "__main__":
|
155 |
+
demo.launch()
|