Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116# 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(page_title="DeepSeek Chatbot - ruslanmv.com",page_icon="🤖",layout="centered")# Initialize session state for chat historyif "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:"# Make API requestresponse = 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}})# Handle API errorsif response.status_code != 200:error_msg = response.json().get('error', 'Unknown API error')st.error(f"API Error: {error_msg}")if "loading" in error_msg.lower():st.info("Please wait a moment and try again. The model might be loading.")return# Process successful responseresult = response.json()if isinstance(result, list):# Handle normal response formatassistant_response = result[0].get('generated_text', 'No response generated')# Clean up responseif "Assistant:" in assistant_response:assistant_response = assistant_response.split("Assistant:")[-1].strip()elif isinstance(result, dict) and 'error' in result:# Handle error formatst.error(f"API Error: {result['error']}")returnelse:st.error("Unexpected response format from API")returnwith 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"Application Error: {str(e)}")12345678910111213141516171819202122232425262728293031import streamlit as stimport requestsimport jsonst.title("DeepSeek-R1-Distill-Qwen-32B")# The Inference API endpoint for your modelAPI_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"# If your model is public, you can often omit the token.# If it is private or rate-limited, you need to provide a token:# headers = {"Authorization": "Bearer YOUR_HF_INFERENCE_API_TOKEN"}headers = {}def query_hf_api(prompt: str):"""Sends a JSON payload to the HF Inference API."""payload = {"inputs": prompt}response = requests.post(API_URL, headers=headers, data=json.dumps(payload))return response.json()# Simple text box for user inputuser_input = st.text_input("Enter your prompt", value="Hello, how are you?")# Generate buttonif st.button("Generate"):with st.spinner("Generating..."):result = query_hf_api(user_input)st.write("**API Response:**")st.json(result)
app.py+30-115