File size: 3,395 Bytes
e10cc1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# app.py
import streamlit as st
from huggingface_hub import InferenceClient
from datetime import datetime

# Configure page
st.set_page_config(
    page_title="DeepSeek Chatbot - ruslanmv.com",
    page_icon="πŸ€–",
    layout="centered",
    initial_sidebar_state="expanded"
)

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

# Sidebar controls
with st.sidebar:
    st.title("πŸ€– Chatbot Settings")
    st.markdown("Created by [ruslanmv.com](https://ruslanmv.com/)")
    
    # Model selection
    selected_model = st.selectbox(
        "Choose Model",
        options=[
            "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
            "deepseek-ai/DeepSeek-R1",
            "deepseek-ai/DeepSeek-R1-Zero"
        ],
        index=0
    )
    
    # System message
    system_message = st.text_area(
        "System Message",
        value="You are a friendly Chatbot created by ruslanmv.com",
        height=100
    )
    
    # Generation parameters
    max_new_tokens = st.slider(
        "Max new tokens",
        min_value=1,
        max_value=4000,
        value=512,
        step=50
    )
    
    temperature = st.slider(
        "Temperature",
        min_value=0.1,
        max_value=4.0,
        value=1.0,
        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
    )
    
    # Optional HF Token
    hf_token = st.text_input(
        "HuggingFace Token (optional)",
        type="password",
        help="Enter your HuggingFace token if required for model access"
    )

# Main chat interface
st.title("πŸ’¬ DeepSeek Chatbot")
st.caption("πŸš€ A conversational AI powered by DeepSeek models")

# Display chat messages
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])
        if "timestamp" in message:
            st.caption(f"_{message['timestamp']}_")

# Chat input and processing
if prompt := st.chat_input("Type your message..."):
    # Add user message to history
    st.session_state.messages.append({
        "role": "user",
        "content": prompt,
        "timestamp": datetime.now().strftime("%H:%M:%S")
    })
    
    # Display user message
    with st.chat_message("user"):
        st.markdown(prompt)
        st.caption(f"_{st.session_state.messages[-1]['timestamp']}_")

    # Create full prompt with system message
    full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
    
    # Create client and generate response
    client = InferenceClient(model=selected_model, token=hf_token)
    
    # Display assistant response
    with st.chat_message("assistant"):
        response = st.write_stream(
            client.text_generation(
                full_prompt,
                max_new_tokens=max_new_tokens,
                temperature=temperature,
                top_p=top_p,
                stream=True
            )
        )
        timestamp = datetime.now().strftime("%H:%M:%S")
        st.caption(f"_{timestamp}_")

    # Add assistant response to history
    st.session_state.messages.append({
        "role": "assistant",
        "content": response,
        "timestamp": timestamp
    })

# Optional debug information
# st.sidebar.markdown("---")
# st.sidebar.json(st.session_state.messages)