ruslanmv commited on
Commit
13df882
·
verified ·
1 Parent(s): 3cb63a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -20
app.py CHANGED
@@ -20,10 +20,12 @@ if "messages" not in st.session_state:
20
  # Sidebar configuration
21
  with st.sidebar:
22
  st.header("Model Configuration")
 
23
 
24
  # Dropdown to select model
25
  model_options = [
26
  "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
 
27
  ]
28
  selected_model = st.selectbox("Select Model", model_options, index=0)
29
 
@@ -53,13 +55,8 @@ def query(payload, api_url):
53
  headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}
54
  logger.info(f"Sending request to {api_url} with payload: {payload}")
55
  response = requests.post(api_url, headers=headers, json=payload)
56
- try:
57
- response.raise_for_status()
58
- logger.info(f"Received response: {response.status_code}, {response.text}")
59
- return response.json()
60
- except requests.exceptions.RequestException as e:
61
- logger.error(f"HTTP Request failed: {e}")
62
- return None
63
 
64
  # Chat interface
65
  st.title("🤖 DeepSeek Chatbot")
@@ -81,23 +78,15 @@ if prompt := st.chat_input("Type your message..."):
81
  with st.spinner("Generating response..."):
82
  # Prepare the payload for the API
83
  payload = {
84
- "inputs": {
85
- "past_user_inputs": [msg["content"] for msg in st.session_state.messages if msg["role"] == "user"],
86
- "generated_responses": [msg["content"] for msg in st.session_state.messages if msg["role"] == "assistant"],
87
- "text": prompt
88
- },
89
  "parameters": {
90
  "max_new_tokens": max_tokens,
91
  "temperature": temperature,
92
  "top_p": top_p,
93
  "return_full_text": False
94
- },
95
- "system_prompt": system_message
96
  }
97
 
98
- # Log the full message parsed to the API
99
- logger.info(f"Full message sent to API: {payload}")
100
-
101
  # Dynamically construct the API URL based on the selected model
102
  api_url = f"https://api-inference.huggingface.co/models/{selected_model}"
103
  logger.info(f"Selected model: {selected_model}, API URL: {api_url}")
@@ -106,7 +95,7 @@ if prompt := st.chat_input("Type your message..."):
106
  output = query(payload, api_url)
107
 
108
  # Handle API response
109
- if output and isinstance(output, list) and len(output) > 0 and 'generated_text' in output[0]:
110
  assistant_response = output[0]['generated_text']
111
  logger.info(f"Generated response: {assistant_response}")
112
 
@@ -115,9 +104,9 @@ if prompt := st.chat_input("Type your message..."):
115
 
116
  st.session_state.messages.append({"role": "assistant", "content": assistant_response})
117
  else:
118
- logger.error(f"Unexpected API response or error: {output}")
119
  st.error("Error: Unable to generate a response. Please try again.")
120
 
121
  except Exception as e:
122
  logger.error(f"Application Error: {str(e)}", exc_info=True)
123
- st.error(f"Application Error: {str(e)}")
 
20
  # Sidebar configuration
21
  with st.sidebar:
22
  st.header("Model Configuration")
23
+ st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)")
24
 
25
  # Dropdown to select model
26
  model_options = [
27
  "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
28
+
29
  ]
30
  selected_model = st.selectbox("Select Model", model_options, index=0)
31
 
 
55
  headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}
56
  logger.info(f"Sending request to {api_url} with payload: {payload}")
57
  response = requests.post(api_url, headers=headers, json=payload)
58
+ logger.info(f"Received response: {response.status_code}, {response.text}")
59
+ return response.json()
 
 
 
 
 
60
 
61
  # Chat interface
62
  st.title("🤖 DeepSeek Chatbot")
 
78
  with st.spinner("Generating response..."):
79
  # Prepare the payload for the API
80
  payload = {
81
+ "inputs": prompt,
 
 
 
 
82
  "parameters": {
83
  "max_new_tokens": max_tokens,
84
  "temperature": temperature,
85
  "top_p": top_p,
86
  "return_full_text": False
87
+ }
 
88
  }
89
 
 
 
 
90
  # Dynamically construct the API URL based on the selected model
91
  api_url = f"https://api-inference.huggingface.co/models/{selected_model}"
92
  logger.info(f"Selected model: {selected_model}, API URL: {api_url}")
 
95
  output = query(payload, api_url)
96
 
97
  # Handle API response
98
+ if isinstance(output, list) and len(output) > 0 and 'generated_text' in output[0]:
99
  assistant_response = output[0]['generated_text']
100
  logger.info(f"Generated response: {assistant_response}")
101
 
 
104
 
105
  st.session_state.messages.append({"role": "assistant", "content": assistant_response})
106
  else:
107
+ logger.error(f"Unexpected API response: {output}")
108
  st.error("Error: Unable to generate a response. Please try again.")
109
 
110
  except Exception as e:
111
  logger.error(f"Application Error: {str(e)}", exc_info=True)
112
+ st.error(f"Application Error: {str(e)}")