Chae
fix code font
a187d8f
# chat.py
import streamlit as st
from huggingface_hub import InferenceClient
import os
import json
st.header("Multi-turn feedback chatbot demo")
# import the inference client
hf_token = os.getenv("HF_TOKEN")
client = InferenceClient(model="openai/gpt-oss-20b", token=hf_token)
#### Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
#### set basic parameters
# hard-coded for now but can be changed
max_tokens = 60000
temperature = 0.7
top_p = 0.95
if "chat_history" not in st.session_state:
st.session_state.chat_history = [{"role": "assistant", "content": "Ask me anything!"}]
# Display chat
for message in st.session_state.chat_history:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("Send a message"):
# add message to chat history
st.session_state.chat_history.append({"role": "user", "content": prompt})
# show message
with st.chat_message("user"):
st.markdown(prompt) # for markdown of user text
# Prepare messages for API call
messages_for_api = [{"role": "system", "content": "You are a helpful assistant."}]
messages_for_api.extend(st.session_state.chat_history)
# Generate and display assistant response
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
try:
# Call Hugging Face API
response = client.chat_completion(messages=messages_for_api,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
stream=False,
)
# show the response
reply = response.choices[0].message["content"]
st.markdown(reply)
# print(reply)
# Add assistant response to chat history
st.session_state.chat_history.append(
{"role": "assistant", "content": reply}
)
except Exception as e:
st.error(f"Error generating response: {str(e)}")
# Don't add the user message if there was an error
st.session_state.chat_history.pop()