Update app.py
Browse files
app.py
CHANGED
@@ -32,7 +32,10 @@ GREETING_MESSAGES = [
|
|
32 |
def get_random_greeting():
|
33 |
return random.choice(GREETING_MESSAGES)
|
34 |
|
35 |
-
|
|
|
|
|
|
|
36 |
messages = [{"role": "system", "content": system_message}]
|
37 |
for user_msg, assistant_msg in history:
|
38 |
if user_msg:
|
@@ -40,63 +43,30 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
40 |
if assistant_msg:
|
41 |
messages.append({"role": "assistant", "content": assistant_msg})
|
42 |
messages.append({"role": "user", "content": message})
|
43 |
-
|
44 |
try:
|
45 |
# Stream response from LLM
|
46 |
stream = llm.create_chat_completion(
|
47 |
messages=messages,
|
48 |
-
max_tokens=
|
49 |
-
temperature=
|
50 |
-
top_p=
|
51 |
stream=True # Enable streaming
|
52 |
)
|
|
|
|
|
53 |
response_content = ""
|
54 |
for chunk in stream:
|
55 |
response_content += chunk["choices"][0]["delta"]["content"]
|
56 |
-
yield response_content
|
57 |
except Exception as e:
|
58 |
yield f"Error: {e}"
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
return [("", greeting_message)], ""
|
63 |
-
|
64 |
-
# Gradio Interface
|
65 |
-
with gr.Blocks() as demo:
|
66 |
-
gr.HTML("<div class='header-text'>AstroSage-LLAMA-3.1-8B</div><div class='subheader'>Astronomy-Specialized Chatbot</div>")
|
67 |
-
|
68 |
-
chatbot = gr.Chatbot(height=400)
|
69 |
-
msg = gr.Textbox(placeholder="Ask about astronomy, astrophysics, or cosmology...", show_label=False)
|
70 |
-
|
71 |
-
with gr.Accordion("Advanced Settings", open=False) as advanced_settings:
|
72 |
-
system_msg = gr.Textbox(
|
73 |
-
value="You are AstroSage, a highly knowledgeable AI assistant specialized in astronomy, astrophysics, and cosmology. Provide accurate, engaging, and educational responses about space science and the universe.",
|
74 |
-
label="System Message",
|
75 |
-
lines=3
|
76 |
-
)
|
77 |
-
max_tokens = gr.Slider(1, 2048, value=512, step=1, label="Max Tokens")
|
78 |
-
temperature = gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature")
|
79 |
-
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p")
|
80 |
-
|
81 |
-
# Automatically handle submission on Enter key press with streaming
|
82 |
-
def handle_submit(message, history, system_message, max_tokens, temperature, top_p):
|
83 |
-
history.append((message, None)) # Append user's message first
|
84 |
-
|
85 |
-
# Stream the assistant's response and update the history
|
86 |
-
for response in respond(message, history, system_message, max_tokens, temperature, top_p):
|
87 |
-
history[-1] = (message, response)
|
88 |
-
yield history, "" # Yield updated history to display in the chatbox
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
handle_submit,
|
93 |
-
inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
94 |
-
outputs=[chatbot, msg],
|
95 |
-
queue=False
|
96 |
-
)
|
97 |
-
|
98 |
-
# Automatically clear context on reload with a greeting
|
99 |
-
demo.load(lambda: clear_context(), None, [chatbot, msg])
|
100 |
|
101 |
if __name__ == "__main__":
|
102 |
-
|
|
|
32 |
def get_random_greeting():
|
33 |
return random.choice(GREETING_MESSAGES)
|
34 |
|
35 |
+
# Function to handle the chat response with streaming
|
36 |
+
def respond_stream(message, history):
|
37 |
+
# Add the system message and previous chat history
|
38 |
+
system_message = "You are AstroSage, a highly knowledgeable AI assistant specialized in astronomy, astrophysics, and cosmology. Provide accurate, engaging, and educational responses about space science and the universe."
|
39 |
messages = [{"role": "system", "content": system_message}]
|
40 |
for user_msg, assistant_msg in history:
|
41 |
if user_msg:
|
|
|
43 |
if assistant_msg:
|
44 |
messages.append({"role": "assistant", "content": assistant_msg})
|
45 |
messages.append({"role": "user", "content": message})
|
46 |
+
|
47 |
try:
|
48 |
# Stream response from LLM
|
49 |
stream = llm.create_chat_completion(
|
50 |
messages=messages,
|
51 |
+
max_tokens=512,
|
52 |
+
temperature=0.7,
|
53 |
+
top_p=0.9,
|
54 |
stream=True # Enable streaming
|
55 |
)
|
56 |
+
|
57 |
+
# Stream the chunks of the response
|
58 |
response_content = ""
|
59 |
for chunk in stream:
|
60 |
response_content += chunk["choices"][0]["delta"]["content"]
|
61 |
+
yield response_content
|
62 |
except Exception as e:
|
63 |
yield f"Error: {e}"
|
64 |
|
65 |
+
# Using gr.ChatInterface for a simpler chat UI
|
66 |
+
chatbot = gr.ChatInterface(fn=respond_stream, type="messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# Set a welcome message
|
69 |
+
chatbot.set_welcome_message(get_random_greeting())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
if __name__ == "__main__":
|
72 |
+
chatbot.launch()
|