A (very) simple, text only chatbot using Society AI inference endpoint
Update app.py
All checks were successful
society-ai-hub-container-cache Actions Demo / build (push) Successful in 1m13s
All checks were successful
society-ai-hub-container-cache Actions Demo / build (push) Successful in 1m13s
This commit is contained in:
parent
5854002ff0
commit
4c63f13b8f
39
app.py
39
app.py
@ -11,41 +11,46 @@ with gr.Blocks() as demo:
|
|||||||
msg = gr.Textbox()
|
msg = gr.Textbox()
|
||||||
clear = gr.Button("Clear")
|
clear = gr.Button("Clear")
|
||||||
|
|
||||||
def user(user_message, history: list):
|
def chat(user_message, history: list):
|
||||||
"""Appends the user message to the conversation history."""
|
"""
|
||||||
return "", history + [{"role": "user", "content": user_message}]
|
Consolidated function that:
|
||||||
|
1) Appends the user message to the conversation history
|
||||||
|
2) Sends the conversation history to the vLLM API
|
||||||
|
3) Streams back the assistant's response
|
||||||
|
"""
|
||||||
|
|
||||||
def bot(history: list):
|
# 1) Append the user message
|
||||||
"""Sends the conversation history to the vLLM API and streams the assistant's response."""
|
history = history + [{"role": "user", "content": user_message}]
|
||||||
# Append an empty assistant message to history to fill in as we receive the response
|
|
||||||
|
# 2) Prepare an empty assistant entry for streaming
|
||||||
history.append({"role": "assistant", "content": ""})
|
history.append({"role": "assistant", "content": ""})
|
||||||
|
|
||||||
|
# 3) Stream the assistant's response
|
||||||
try:
|
try:
|
||||||
# Create a chat completion with streaming enabled using the client
|
|
||||||
completion = client.chat.completions.create(
|
completion = client.chat.completions.create(
|
||||||
model="llama-3.2-3B-instruct", # Adjust the model name if needed
|
model="llama-3.2-3B-instruct",
|
||||||
messages=history,
|
messages=history,
|
||||||
stream=True
|
stream=True
|
||||||
)
|
)
|
||||||
|
|
||||||
# Iterate over the streamed response
|
|
||||||
for chunk in completion:
|
for chunk in completion:
|
||||||
# Access the delta content from the chunk
|
|
||||||
delta = chunk.choices[0].delta
|
delta = chunk.choices[0].delta
|
||||||
content = getattr(delta, 'content', '')
|
content = getattr(delta, 'content', '')
|
||||||
if content:
|
if content:
|
||||||
# Update the assistant's message with new content
|
|
||||||
history[-1]['content'] += content
|
history[-1]['content'] += content
|
||||||
yield history
|
# Yield both the cleared textbox ("") and updated history
|
||||||
|
yield "", history
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Handle exceptions and display an error message
|
# Handle exceptions and display an error message
|
||||||
history[-1]['content'] += f"\n[Error]: {str(e)}"
|
history[-1]['content'] += f"\n[Error]: {str(e)}"
|
||||||
yield history
|
yield "", history
|
||||||
|
|
||||||
# Set up the Gradio interface components
|
# Wire up the single chat function to the UI
|
||||||
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
msg.submit(
|
||||||
bot, chatbot, chatbot
|
fn=chat, # single consolidated function
|
||||||
|
inputs=[msg, chatbot], # pass user message and current chatbot history
|
||||||
|
outputs=[msg, chatbot] # clear the message box and update the chatbot
|
||||||
)
|
)
|
||||||
|
|
||||||
clear.click(lambda: None, None, chatbot, queue=False)
|
clear.click(lambda: None, None, chatbot, queue=False)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user