File size: 8,677 Bytes
94d49c9 0f6901c 91f7b07 268ed5c 94d49c9 3f3749a 94d49c9 13df882 94d49c9 268ed5c 91f7b07 268ed5c 94d49c9 91f7b07 94d49c9 13df882 649e09f 94d49c9 268ed5c 91f7b07 268ed5c 91f7b07 268ed5c 0f6901c 91f7b07 94d49c9 268ed5c 91f7b07 f0074ad a4d6b56 91f7b07 a4d6b56 268ed5c 0f6901c 268ed5c 94d49c9 649e09f 94d49c9 13df882 94d49c9 268ed5c 94d49c9 268ed5c 94d49c9 268ed5c 91f7b07 649e09f 2e44d20 91f7b07 268ed5c 2e44d20 268ed5c 649e09f 2e44d20 268ed5c 2e44d20 649e09f 94d49c9 649e09f 268ed5c 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
import streamlit as st
import requests
import logging
import json
import re
from datetime import datetime, timedelta
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
# --- Optional: translate_query ๋ฐ ๊ด๋ จ ์์ ์ ์ (์ค์ ๊ตฌํ์ ๋ง๊ฒ ์์ ) ---
def translate_query(query, country):
# ์ค์ ์ํฉ์์๋ ๋ฒ์ญ ๋ก์ง์ ๊ตฌํํ์ธ์.
return query
COUNTRY_LOCATIONS = {
"United States": "United States",
"South Korea": "South Korea"
}
COUNTRY_LANGUAGES = {
"United States": "en",
"South Korea": "ko"
}
# -------------------------------------------------------------------------
# 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
# --- ์์ ๋ ์น์์น ๊ธฐ๋ฅ ---
def search_web(query, country="United States", page=1, num_result=10):
url = "https://api.serphouse.com/serp/live"
# ์ต๊ทผ 24์๊ฐ ๊ธฐ๊ฐ ์ค์
now = datetime.utcnow()
yesterday = now - timedelta(days=1)
date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
# ์ฟผ๋ฆฌ ๋ฒ์ญ (ํ์์)
translated_query = translate_query(query, country)
payload = {
"data": {
"q": translated_query,
"domain": "google.com",
"loc": COUNTRY_LOCATIONS.get(country, "United States"),
"lang": COUNTRY_LANGUAGES.get(country, "en"),
"device": "desktop",
"serp_type": "web", # ์ํ๋ ๊ฒฝ์ฐ "news" ๋ฑ์ผ๋ก ๋ณ๊ฒฝ ๊ฐ๋ฅ
"page": str(page),
"num": str(num_result),
"date_range": date_range,
"sort_by": "date"
}
}
api_key = st.secrets.get("SERPHOUSE_API_TOKEN")
if not api_key:
logger.error("SERPHOUSE_API_TOKEN not found in st.secrets")
return {"error": "API token not configured."}
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {api_key}"
}
try:
session = requests.Session()
retries = Retry(
total=5,
backoff_factor=1,
status_forcelist=[500, 502, 503, 504, 429],
allowed_methods=["POST"]
)
adapter = HTTPAdapter(max_retries=retries)
session.mount('http://', adapter)
session.mount('https://', adapter)
response = session.post(
url,
json=payload,
headers=headers,
timeout=(30, 30) # ์ฐ๊ฒฐ ๋ฐ ์ฝ๊ธฐ ํ์์์ 30์ด์ฉ
)
response.raise_for_status()
return {"results": response.json(), "translated_query": translated_query}
except requests.exceptions.Timeout:
return {
"error": "๊ฒ์ ์๊ฐ์ด ์ด๊ณผ๋์์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์.",
"translated_query": query
}
except requests.exceptions.RequestException as e:
return {
"error": f"๊ฒ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}",
"translated_query": query
}
except Exception as e:
return {
"error": f"์๊ธฐ์น ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}",
"translated_query": query
}
# --- ๋ ---
# ๋ด๋ถ ์ฒด์ธ ์ค๋ธ ์(์๊ฐ ๊ณผ์ ) ์ ๊ฑฐ ํจ์
def remove_chain_of_thought(text):
cleaned_text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
return cleaned_text.strip()
# 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..."):
# ์
๋ฐ์ดํธ๋ ์น์์น ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ๊ฒ์ ์ํ
search_results = search_web(prompt, country="United States", page=1, num_result=10)
# ๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ
if search_results and "results" in search_results:
if 'organic' in search_results["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"
else:
search_content = "Sorry, no relevant search results found.\n\n"
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
}
}
# ์ ํํ ๋ชจ๋ธ์ ๋ฐ๋ฅธ API URL ๋์ ๊ตฌ์ฑ
api_url = f"https://api-inference.huggingface.co/models/{selected_model}"
logger.info(f"Selected model: {selected_model}, API URL: {api_url}")
# Hugging Face API์ ์ฟผ๋ฆฌ ์ ์ก
output = query(payload, api_url)
# API ์๋ต ์ฒ๋ฆฌ
if output is not None and isinstance(output, list) and len(output) > 0:
if 'generated_text' in output[0]:
assistant_response = output[0]['generated_text'].strip()
# ๋ด๋ถ ์ฒด์ธ ์ค๋ธ ์ ์ ๊ฑฐ
unique_response = remove_chain_of_thought(assistant_response)
logger.info(f"Generated response: {unique_response}")
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)}")
|