Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,116 +1,31 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
import os
|
| 3 |
-
import requests
|
| 4 |
import streamlit as st
|
| 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 |
-
1, 4000, 512
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
temperature = st.slider(
|
| 39 |
-
"Temperature",
|
| 40 |
-
0.1, 4.0, 0.7
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
top_p = st.slider(
|
| 44 |
-
"Top-p",
|
| 45 |
-
0.1, 1.0, 0.9
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
# Chat interface
|
| 49 |
-
st.title("🤖 DeepSeek Chatbot")
|
| 50 |
-
st.caption("Powered by Hugging Face Inference API - Configure in sidebar")
|
| 51 |
-
|
| 52 |
-
# Display chat history
|
| 53 |
-
for message in st.session_state.messages:
|
| 54 |
-
with st.chat_message(message["role"]):
|
| 55 |
-
st.markdown(message["content"])
|
| 56 |
-
|
| 57 |
-
# Handle input
|
| 58 |
-
if prompt := st.chat_input("Type your message..."):
|
| 59 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 60 |
-
|
| 61 |
-
with st.chat_message("user"):
|
| 62 |
-
st.markdown(prompt)
|
| 63 |
-
|
| 64 |
-
try:
|
| 65 |
-
with st.spinner("Generating response..."):
|
| 66 |
-
full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
|
| 67 |
-
|
| 68 |
-
# Make API request
|
| 69 |
-
response = requests.post(
|
| 70 |
-
API_URL,
|
| 71 |
-
headers=headers,
|
| 72 |
-
json={
|
| 73 |
-
"inputs": full_prompt,
|
| 74 |
-
"parameters": {
|
| 75 |
-
"max_new_tokens": max_tokens,
|
| 76 |
-
"temperature": temperature,
|
| 77 |
-
"top_p": top_p,
|
| 78 |
-
"return_full_text": False
|
| 79 |
-
}
|
| 80 |
-
}
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
# Handle API errors
|
| 84 |
-
if response.status_code != 200:
|
| 85 |
-
error_msg = response.json().get('error', 'Unknown API error')
|
| 86 |
-
st.error(f"API Error: {error_msg}")
|
| 87 |
-
if "loading" in error_msg.lower():
|
| 88 |
-
st.info("Please wait a moment and try again. The model might be loading.")
|
| 89 |
-
return
|
| 90 |
-
|
| 91 |
-
# Process successful response
|
| 92 |
-
result = response.json()
|
| 93 |
-
|
| 94 |
-
if isinstance(result, list):
|
| 95 |
-
# Handle normal response format
|
| 96 |
-
assistant_response = result[0].get('generated_text', 'No response generated')
|
| 97 |
-
|
| 98 |
-
# Clean up response
|
| 99 |
-
if "Assistant:" in assistant_response:
|
| 100 |
-
assistant_response = assistant_response.split("Assistant:")[-1].strip()
|
| 101 |
-
|
| 102 |
-
elif isinstance(result, dict) and 'error' in result:
|
| 103 |
-
# Handle error format
|
| 104 |
-
st.error(f"API Error: {result['error']}")
|
| 105 |
-
return
|
| 106 |
-
else:
|
| 107 |
-
st.error("Unexpected response format from API")
|
| 108 |
-
return
|
| 109 |
-
|
| 110 |
-
with st.chat_message("assistant"):
|
| 111 |
-
st.markdown(assistant_response)
|
| 112 |
-
|
| 113 |
-
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
| 114 |
-
|
| 115 |
-
except Exception as e:
|
| 116 |
-
st.error(f"Application Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
st.title("DeepSeek-R1-Distill-Qwen-32B")
|
| 6 |
+
|
| 7 |
+
# The Inference API endpoint for your model
|
| 8 |
+
API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
|
| 9 |
+
|
| 10 |
+
# If your model is public, you can often omit the token.
|
| 11 |
+
# If it is private or rate-limited, you need to provide a token:
|
| 12 |
+
# headers = {"Authorization": "Bearer YOUR_HF_INFERENCE_API_TOKEN"}
|
| 13 |
+
headers = {}
|
| 14 |
+
|
| 15 |
+
def query_hf_api(prompt: str):
|
| 16 |
+
"""
|
| 17 |
+
Sends a JSON payload to the HF Inference API.
|
| 18 |
+
"""
|
| 19 |
+
payload = {"inputs": prompt}
|
| 20 |
+
response = requests.post(API_URL, headers=headers, data=json.dumps(payload))
|
| 21 |
+
return response.json()
|
| 22 |
+
|
| 23 |
+
# Simple text box for user input
|
| 24 |
+
user_input = st.text_input("Enter your prompt", value="Hello, how are you?")
|
| 25 |
+
|
| 26 |
+
# Generate button
|
| 27 |
+
if st.button("Generate"):
|
| 28 |
+
with st.spinner("Generating..."):
|
| 29 |
+
result = query_hf_api(user_input)
|
| 30 |
+
st.write("**API Response:**")
|
| 31 |
+
st.json(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|