Tijmen2 commited on
Commit
43a1946
·
verified ·
1 Parent(s): b39c68e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -33
app.py CHANGED
@@ -29,39 +29,43 @@ GREETING_MESSAGES = [
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 = """
@@ -103,7 +107,8 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="indigo", neutra
103
  label="Chat with AstroSage",
104
  bubble_full_width=False,
105
  show_label=True,
106
- height=450
 
107
  )
108
 
109
  with gr.Row():
@@ -127,26 +132,26 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="indigo", neutra
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
 
 
29
  "The universe awaits! I'm AstroSage. What astronomical wonders shall we discuss?",
30
  ]
31
 
32
+ def user(user_message, history):
33
+ """Add user message to chat history."""
34
+ return "", history + [{"role": "user", "content": user_message}]
 
 
 
 
 
35
 
36
+ def bot(history):
37
+ """Generate and stream the bot's response."""
38
+ # Prepare the messages for the model
39
+ messages = [
40
+ {
41
+ "role": "system",
42
+ "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."
43
+ }
44
+ ]
45
 
46
+ # Add chat history
47
+ for message in history[:-1]: # Exclude the last message which we just added
48
+ messages.append({"role": message["role"], "content": message["content"]})
49
 
50
+ # Add the current user message
51
+ messages.append({"role": "user", "content": history[-1]["content"]})
52
+
53
+ # Start generating the response
54
+ history.append({"role": "assistant", "content": ""})
55
+
56
+ # Stream the response
57
  response = llm.create_chat_completion(
58
+ messages=messages,
 
 
 
 
59
  max_tokens=512,
60
  temperature=0.7,
61
  top_p=0.95,
62
  stream=True
63
  )
64
 
 
65
  for chunk in response:
66
  if chunk and "content" in chunk["choices"][0]["delta"]:
67
+ history[-1]["content"] += chunk["choices"][0]["delta"]["content"]
68
+ yield history
69
 
70
  # Custom CSS for a space theme
71
  custom_css = """
 
107
  label="Chat with AstroSage",
108
  bubble_full_width=False,
109
  show_label=True,
110
+ height=450,
111
+ type="messages"
112
  )
113
 
114
  with gr.Row():
 
132
  label="Example Questions"
133
  )
134
 
135
+ # Set up the message chain with streaming
136
  msg.submit(
137
+ user,
138
  [msg, chatbot],
139
  [msg, chatbot],
 
 
 
 
 
 
 
140
  queue=False
141
+ ).then(
142
+ bot,
143
+ chatbot,
144
+ chatbot
145
  )
146
 
147
+ # Clear button functionality
148
+ clear.click(lambda: None, None, chatbot, queue=False)
149
+
150
  # Initial greeting
151
  demo.load(
152
+ lambda: [[{"role": "assistant", "content": random.choice(GREETING_MESSAGES)}]],
153
  None,
154
+ chatbot,
155
  queue=False
156
  )
157