File size: 5,808 Bytes
94d49c9
 
 
0f6901c
94d49c9
 
 
 
 
 
 
3f3749a
94d49c9
 
 
 
 
 
 
 
 
 
 
13df882
94d49c9
 
 
 
 
 
 
 
 
3f3749a
94d49c9
 
 
 
 
2a16e69
94d49c9
 
 
 
f6905f3
94d49c9
 
 
 
f6905f3
94d49c9
 
 
 
 
 
 
13df882
649e09f
 
 
 
 
94d49c9
f0074ad
0f6901c
f0074ad
0f6901c
 
 
 
 
 
 
94d49c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f6901c
 
 
 
f0074ad
 
0f6901c
 
 
 
 
 
 
94d49c9
649e09f
94d49c9
 
 
 
 
13df882
94d49c9
 
 
 
 
2e44d20
94d49c9
 
 
 
649e09f
 
2e44d20
 
 
 
 
 
 
 
649e09f
2e44d20
649e09f
2e44d20
649e09f
2e44d20
649e09f
 
 
94d49c9
649e09f
 
94d49c9
 
 
2e44d20
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import streamlit as st
import requests
import logging
import json

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

# Page configuration
st.set_page_config(
    page_title="DeepSeek Chatbot",
    page_icon="🤖",
    layout="centered"
)

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

# Sidebar configuration
with st.sidebar:
    st.header("Model Configuration")
    st.markdown("[Get HuggingFace Token](https://huggingface.co./settings/tokens)")

    # Dropdown to select model
    model_options = [
        "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
    ]
    selected_model = st.selectbox("Select Model", model_options, index=0)

    system_message = st.text_area(
        "System Message",
        value="You are a deep thinking AI, you may use extremely long chains of thought to deeply consider the problem and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. You should enclose your thoughts and internal monologue inside tags, and then provide your solution or response to the problem.",
        height=100
    )

    max_tokens = st.slider(
        "Max Tokens",
        10, 4000, 1000
    )

    temperature = st.slider(
        "Temperature",
        0.1, 4.0, 0.3
    )

    top_p = st.slider(
        "Top-p",
        0.1, 1.0, 0.6
    )

# Function to query the Hugging Face API
def query(payload, api_url):
    headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}
    logger.info(f"Sending request to {api_url} with payload: {payload}")
    response = requests.post(api_url, headers=headers, json=payload)
    logger.info(f"Received response: {response.status_code}, {response.text}")
    try:
        return response.json()
    except requests.exceptions.JSONDecodeError:
        logger.error(f"Failed to decode JSON response: {response.text}")
        return None

# Function to perform a web search using SerpHouse API
def search_web(query):
    search_url = f"https://api.serphouse.com/serp/live?api_token=V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6&q={query}&responseType=json&domain=google&lang=en&serp_type=web"
    response = requests.get(search_url)
    if response.status_code == 200:
        return response.json()
    else:
        logger.error(f"Error while searching: {response.status_code}")
        return None

# Chat interface
st.title("🤖 DeepSeek Chatbot")
st.caption("Powered by Hugging Face Inference API - Configure in sidebar")

# Display chat history
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Handle input
if 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..."):
            # Perform a web search based on the user input
            search_results = search_web(prompt)

            # Process search results if available
            if search_results and "results" in search_results:
                search_content = "\n".join([f"**{item['title']}**: {item['snippet']}" for item in search_results["results"]["organic"]])
                search_content = f"Here are some search results related to your question:\n\n{search_content}\n\n"

                # Combine the system message, search results, and user input into a single prompt
                full_prompt = f"{system_message}\n\n{search_content}User: {prompt}\nAssistant:"
            else:
                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
                }
            }

            # Dynamically construct the API URL based on the selected model
            api_url = f"https://api-inference.huggingface.co/models/{selected_model}"
            logger.info(f"Selected model: {selected_model}, API URL: {api_url}")

            # Query the Hugging Face API using the selected model
            output = query(payload, api_url)

            # Handle API response
            if output is not None and isinstance(output, list) and len(output) > 0:
                if 'generated_text' in output[0]:
                    # Extract the assistant's response
                    assistant_response = output[0]['generated_text'].strip()

                    # Check for and remove duplicate responses
                    responses = assistant_response.split("\n</think>\n")
                    unique_response = responses[0].strip()

                    logger.info(f"Generated response: {unique_response}")

                    # Append response to chat only once
                    with st.chat_message("assistant"):
                        st.markdown(unique_response)

                    st.session_state.messages.append({"role": "assistant", "content": unique_response})
                else:
                    logger.error(f"Unexpected API response structure: {output}")
                    st.error("Error: Unexpected response from the model. Please try again.")
            else:
                logger.error(f"Empty or invalid API response: {output}")
                st.error("Error: Unable to generate a response. Please check the model and try again.")

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