Tijmen2 commited on
Commit
b39c68e
β€’
1 Parent(s): abe401d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -37
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 respond_stream(message, history):
33
- if not message:
34
- return
35
-
36
- system_message = "Assume the role of AstroSage, a helpful chatbot designed to answer user queries about astronomy, astrophysics, and cosmology."
37
- messages = [{"role": "system", "content": system_message}]
38
- for user, assistant in history:
39
- messages.append({"role": "user", "content": user})
40
  if assistant:
41
- messages.append({"role": "assistant", "content": assistant})
42
- messages.append({"role": "user", "content": message})
43
 
44
- try:
45
- past_tokens = "" # Accumulate and yield all tokens so far
46
- for chunk in llm.create_chat_completion(
47
- messages=messages,
48
- max_tokens=512,
49
- temperature=0.7,
50
- top_p=0.9,
51
- stream=True
52
- ):
53
- delta = chunk["choices"][0]["delta"]
54
- if "content" in delta:
55
- new_tokens = delta["content"]
56
- past_tokens += new_tokens
57
- yield past_tokens # Yield the accumulated response to allow streaming
58
- except Exception as e:
59
- yield f"Error during generation: {e}"
 
 
 
 
 
 
 
 
60
 
61
- initial_message = random.choice(GREETING_MESSAGES)
62
- chatbot = gr.Chatbot([[None, initial_message]]).style(height=750) # Set height
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  with gr.Row():
66
- with gr.Column(scale=0.8):
67
- chatbot.render()
68
-
69
- with gr.Column(scale=0.2):
70
- clear = gr.Button("Clear")
71
-
72
- clear.click(lambda: [], None, chatbot,queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
- demo.queue().launch()
 
 
 
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()