File size: 3,120 Bytes
7cc1093
 
 
 
 
 
 
 
23bfc7b
7cc1093
6afcb0b
7cc1093
23bfc7b
7cc1093
 
 
 
 
 
6afcb0b
 
7cc1093
6afcb0b
 
 
7cc1093
6afcb0b
 
 
 
 
 
7cc1093
6afcb0b
 
 
 
7cc1093
 
 
 
 
23bfc7b
6afcb0b
7cc1093
23bfc7b
 
7cc1093
 
23bfc7b
7cc1093
6afcb0b
7cc1093
6afcb0b
7cc1093
6afcb0b
 
7cc1093
23bfc7b
6afcb0b
7cc1093
6afcb0b
7cc1093
 
6afcb0b
7cc1093
 
 
 
 
 
 
 
 
 
 
 
 
 
23bfc7b
 
6731784
6afcb0b
23bfc7b
7cc1093
6afcb0b
 
7cc1093
 
6afcb0b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import streamlit as st
import requests
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Set page configuration
st.set_page_config(
    page_title="DeepSeek Chatbot",
    page_icon="πŸ€–",
    layout="wide"
)

# Initialize session state for chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Sidebar for model configuration
st.sidebar.title("βš™οΈ Settings")

# Model selection
model_options = ["deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"]
selected_model = st.sidebar.selectbox("Select AI Model", model_options)

# System message input
system_message = st.sidebar.text_area(
    "System Message",
    value="You are a friendly chatbot. Provide clear and engaging responses.",
    height=80
)

# Chat configuration settings
max_tokens = st.sidebar.slider("Max Tokens", 10, 4000, 300)
temperature = st.sidebar.slider("Temperature", 0.1, 2.0, 0.7)
top_p = st.sidebar.slider("Top-p", 0.1, 1.0, 0.9)

# Function to query the Hugging Face API
def query(payload, api_url):
    headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}
    try:
        response = requests.post(api_url, headers=headers, json=payload)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        logger.error(f"Request Error: {e}")
        return None

# Main Chat Interface
st.title("πŸ€– DeepSeek Chatbot")
st.write("Chat with an AI-powered assistant.")

# Display chat history
for message in st.session_state.messages:
    role = "πŸ§‘β€πŸ’» You" if message["role"] == "user" else "πŸ€– AI"
    st.markdown(f"**{role}:** {message['content']}")

# Handle user input
if prompt := st.chat_input("Type your message..."):
    st.session_state.messages.append({"role": "user", "content": prompt})
    st.markdown(f"**πŸ§‘β€πŸ’» You:** {prompt}")

    try:
        with st.spinner("Generating response..."):
            full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
            payload = {
                "inputs": full_prompt,
                "parameters": {
                    "max_new_tokens": max_tokens,
                    "temperature": temperature,
                    "top_p": top_p,
                    "return_full_text": False
                }
            }

            api_url = f"https://api-inference.huggingface.co/models/{selected_model}"
            output = query(payload, api_url)

            if output and isinstance(output, list) and 'generated_text' in output[0]:
                assistant_response = output[0]['generated_text'].strip()
                assistant_response = assistant_response.replace("</think>", "").strip()
                st.markdown(f"**πŸ€– AI:** {assistant_response}")
                st.session_state.messages.append({"role": "assistant", "content": assistant_response})
            else:
                st.error("Unable to generate a response. Please try again.")

    except Exception as e:
        logger.error(f"Application Error: {str(e)}", exc_info=True)
        st.error(f"Error: {str(e)}")