Update app.py
Browse files1234567896 unmodified lines16171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697# app.pyimport streamlit as stfrom models import load_model# Load the model oncegenerator = load_model()# Page configurationst.set_page_config(6 unmodified linesif "messages" not in st.session_state:st.session_state.messages = []# Sidebar for model parameterswith st.sidebar:st.header("Model Configuration")# System messagesystem_message = st.text_area("System Message",value="You are a friendly Chatbot created by ruslanmv.com",height=100)# Generation parametersmax_tokens = st.slider("Max Tokens",min_value=1,max_value=4000,value=512,step=10)temperature = st.slider("Temperature",min_value=0.1,max_value=4.0,value=0.7,step=0.1)top_p = st.slider("Top-p (nucleus sampling)",min_value=0.1,max_value=1.0,value=0.9,step=0.1)# Main chat interfacest.title("🤖 DeepSeek Chatbot")st.caption("Powered by ruslanmv.com - Configure parameters in the sidebar")# Display chat messagesfor message in st.session_state.messages:with st.chat_message(message["role"]):st.markdown(message["content"])# Chat inputif prompt := st.chat_input("Type your message..."):# Add user message to chat historyst.session_state.messages.append({"role": "user", "content": prompt})# Display user messagewith st.chat_message("user"):st.markdown(prompt)try:# Generate response using the modelwith st.spinner("Generating response..."):full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"response = generator(full_prompt,max_length=max_tokens,temperature=temperature,top_p=top_p,do_sample=True,num_return_sequences=1)[0]['generated_text']# Extract only the assistant's responseassistant_response = response.split("Assistant:")[-1].strip()# Display assistant responsewith st.chat_message("assistant"):st.markdown(assistant_response)# Add assistant response to chat historyst.session_state.messages.append({"role": "assistant", "content": assistant_response})except Exception as e:st.error(f"An error occurred: {str(e)}")1234567891011126 unmodified lines192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293# app.pyimport osimport requestsimport streamlit as stfrom models import get_hf_api# Configure APIAPI_URL = get_hf_api()headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}# Page configurationst.set_page_config(6 unmodified linesif "messages" not in st.session_state:st.session_state.messages = []# Sidebar configurationwith st.sidebar:st.header("Model Configuration")st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)")system_message = st.text_area("System Message",value="You are a friendly Chatbot created by ruslanmv.com",height=100)max_tokens = st.slider("Max Tokens",1, 4000, 512)temperature = st.slider("Temperature",0.1, 4.0, 0.7)top_p = st.slider("Top-p",0.1, 1.0, 0.9)# Chat interfacest.title("🤖 DeepSeek Chatbot")st.caption("Powered by Hugging Face Inference API - Configure in sidebar")# Display chat historyfor message in st.session_state.messages:with st.chat_message(message["role"]):st.markdown(message["content"])# Handle inputif prompt := st.chat_input("Type your message..."):st.session_state.messages.append({"role": "user", "content": prompt})with st.chat_message("user"):st.markdown(prompt)try:with st.spinner("Generating response..."):full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"response = requests.post(API_URL,headers=headers,json={"inputs": full_prompt,"parameters": {"max_new_tokens": max_tokens,"temperature": temperature,"top_p": top_p,"return_full_text": False}}).json()if isinstance(response, list) and len(response) > 0:assistant_response = response[0].get('generated_text', '')else:assistant_response = "Error: Unexpected API response"with st.chat_message("assistant"):st.markdown(assistant_response)st.session_state.messages.append({"role": "assistant", "content": assistant_response})except Exception as e:st.error(f"API Error: {str(e)}")
app.py+38-42