Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
|
|
2 |
import requests
|
3 |
import logging
|
4 |
import json
|
|
|
5 |
from datetime import datetime, timedelta
|
6 |
from requests.adapters import HTTPAdapter
|
7 |
from urllib3.util.retry import Retry
|
@@ -50,27 +51,15 @@ with st.sidebar:
|
|
50 |
system_message = st.text_area(
|
51 |
"System Message",
|
52 |
value=(
|
53 |
-
"You are a deep thinking AI, you may use extremely long chains of thought to deeply consider the problem and "
|
54 |
-
"deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. "
|
55 |
"You should enclose your thoughts and internal monologue inside tags, and then provide your solution or response to the problem."
|
56 |
),
|
57 |
height=100
|
58 |
)
|
59 |
|
60 |
-
max_tokens = st.slider(
|
61 |
-
|
62 |
-
|
63 |
-
)
|
64 |
-
|
65 |
-
temperature = st.slider(
|
66 |
-
"Temperature",
|
67 |
-
0.1, 4.0, 0.3
|
68 |
-
)
|
69 |
-
|
70 |
-
top_p = st.slider(
|
71 |
-
"Top-p",
|
72 |
-
0.1, 1.0, 0.6
|
73 |
-
)
|
74 |
|
75 |
# Function to query the Hugging Face API
|
76 |
def query(payload, api_url):
|
@@ -103,7 +92,7 @@ def search_web(query, country="United States", page=1, num_result=10):
|
|
103 |
"loc": COUNTRY_LOCATIONS.get(country, "United States"),
|
104 |
"lang": COUNTRY_LANGUAGES.get(country, "en"),
|
105 |
"device": "desktop",
|
106 |
-
"serp_type": "web", #
|
107 |
"page": str(page),
|
108 |
"num": str(num_result),
|
109 |
"date_range": date_range,
|
@@ -111,7 +100,6 @@ def search_web(query, country="United States", page=1, num_result=10):
|
|
111 |
}
|
112 |
}
|
113 |
|
114 |
-
# st.secrets์ SERPHOUSE_API_TOKEN์ด ์ ์ฅ๋์ด ์์ด์ผ ํฉ๋๋ค.
|
115 |
api_key = st.secrets.get("SERPHOUSE_API_TOKEN")
|
116 |
if not api_key:
|
117 |
logger.error("SERPHOUSE_API_TOKEN not found in st.secrets")
|
@@ -124,7 +112,6 @@ def search_web(query, country="United States", page=1, num_result=10):
|
|
124 |
}
|
125 |
|
126 |
try:
|
127 |
-
# ์ธ์
๊ณผ ์ฌ์๋ ์ค์
|
128 |
session = requests.Session()
|
129 |
retries = Retry(
|
130 |
total=5,
|
@@ -136,12 +123,11 @@ def search_web(query, country="United States", page=1, num_result=10):
|
|
136 |
session.mount('http://', adapter)
|
137 |
session.mount('https://', adapter)
|
138 |
|
139 |
-
# ์ฐ๊ฒฐ ๋ฐ ์ฝ๊ธฐ ํ์์์ 30์ด์ฉ ์ค์
|
140 |
response = session.post(
|
141 |
url,
|
142 |
json=payload,
|
143 |
headers=headers,
|
144 |
-
timeout=(30, 30)
|
145 |
)
|
146 |
response.raise_for_status()
|
147 |
return {"results": response.json(), "translated_query": translated_query}
|
@@ -163,6 +149,11 @@ def search_web(query, country="United States", page=1, num_result=10):
|
|
163 |
}
|
164 |
# --- ๋ ---
|
165 |
|
|
|
|
|
|
|
|
|
|
|
166 |
# Chat interface
|
167 |
st.title("๐ค DeepSeek Chatbot")
|
168 |
st.caption("Powered by Hugging Face Inference API - Configure in sidebar")
|
@@ -184,11 +175,12 @@ if prompt := st.chat_input("Type your message..."):
|
|
184 |
# ์
๋ฐ์ดํธ๋ ์น์์น ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ๊ฒ์ ์ํ
|
185 |
search_results = search_web(prompt, country="United States", page=1, num_result=10)
|
186 |
|
187 |
-
# ๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ
|
188 |
if search_results and "results" in search_results:
|
189 |
if 'organic' in search_results["results"]:
|
190 |
-
search_content = "\n".join(
|
191 |
-
|
|
|
192 |
search_content = f"Here are some search results related to your question:\n\n{search_content}\n\n"
|
193 |
else:
|
194 |
search_content = "Sorry, no relevant search results found.\n\n"
|
@@ -214,14 +206,12 @@ if prompt := st.chat_input("Type your message..."):
|
|
214 |
# Hugging Face API์ ์ฟผ๋ฆฌ ์ ์ก
|
215 |
output = query(payload, api_url)
|
216 |
|
217 |
-
# ์๋ต ์ฒ๋ฆฌ
|
218 |
if output is not None and isinstance(output, list) and len(output) > 0:
|
219 |
if 'generated_text' in output[0]:
|
220 |
assistant_response = output[0]['generated_text'].strip()
|
221 |
-
|
222 |
-
|
223 |
-
responses = assistant_response.split("\n</think>\n")
|
224 |
-
unique_response = responses[0].strip()
|
225 |
|
226 |
logger.info(f"Generated response: {unique_response}")
|
227 |
|
|
|
2 |
import requests
|
3 |
import logging
|
4 |
import json
|
5 |
+
import re
|
6 |
from datetime import datetime, timedelta
|
7 |
from requests.adapters import HTTPAdapter
|
8 |
from urllib3.util.retry import Retry
|
|
|
51 |
system_message = st.text_area(
|
52 |
"System Message",
|
53 |
value=(
|
54 |
+
"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. "
|
|
|
55 |
"You should enclose your thoughts and internal monologue inside tags, and then provide your solution or response to the problem."
|
56 |
),
|
57 |
height=100
|
58 |
)
|
59 |
|
60 |
+
max_tokens = st.slider("Max Tokens", 10, 4000, 1000)
|
61 |
+
temperature = st.slider("Temperature", 0.1, 4.0, 0.3)
|
62 |
+
top_p = st.slider("Top-p", 0.1, 1.0, 0.6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
# Function to query the Hugging Face API
|
65 |
def query(payload, api_url):
|
|
|
92 |
"loc": COUNTRY_LOCATIONS.get(country, "United States"),
|
93 |
"lang": COUNTRY_LANGUAGES.get(country, "en"),
|
94 |
"device": "desktop",
|
95 |
+
"serp_type": "web", # ์ํ๋ ๊ฒฝ์ฐ "news" ๋ฑ์ผ๋ก ๋ณ๊ฒฝ ๊ฐ๋ฅ
|
96 |
"page": str(page),
|
97 |
"num": str(num_result),
|
98 |
"date_range": date_range,
|
|
|
100 |
}
|
101 |
}
|
102 |
|
|
|
103 |
api_key = st.secrets.get("SERPHOUSE_API_TOKEN")
|
104 |
if not api_key:
|
105 |
logger.error("SERPHOUSE_API_TOKEN not found in st.secrets")
|
|
|
112 |
}
|
113 |
|
114 |
try:
|
|
|
115 |
session = requests.Session()
|
116 |
retries = Retry(
|
117 |
total=5,
|
|
|
123 |
session.mount('http://', adapter)
|
124 |
session.mount('https://', adapter)
|
125 |
|
|
|
126 |
response = session.post(
|
127 |
url,
|
128 |
json=payload,
|
129 |
headers=headers,
|
130 |
+
timeout=(30, 30) # ์ฐ๊ฒฐ ๋ฐ ์ฝ๊ธฐ ํ์์์ 30์ด์ฉ
|
131 |
)
|
132 |
response.raise_for_status()
|
133 |
return {"results": response.json(), "translated_query": translated_query}
|
|
|
149 |
}
|
150 |
# --- ๋ ---
|
151 |
|
152 |
+
# ๋ด๋ถ ์ฒด์ธ ์ค๋ธ ์(์๊ฐ ๊ณผ์ ) ์ ๊ฑฐ ํจ์
|
153 |
+
def remove_chain_of_thought(text):
|
154 |
+
cleaned_text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
|
155 |
+
return cleaned_text.strip()
|
156 |
+
|
157 |
# Chat interface
|
158 |
st.title("๐ค DeepSeek Chatbot")
|
159 |
st.caption("Powered by Hugging Face Inference API - Configure in sidebar")
|
|
|
175 |
# ์
๋ฐ์ดํธ๋ ์น์์น ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ๊ฒ์ ์ํ
|
176 |
search_results = search_web(prompt, country="United States", page=1, num_result=10)
|
177 |
|
178 |
+
# ๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ
|
179 |
if search_results and "results" in search_results:
|
180 |
if 'organic' in search_results["results"]:
|
181 |
+
search_content = "\n".join(
|
182 |
+
[f"**{item['title']}**: {item['snippet']}" for item in search_results["results"]["organic"]]
|
183 |
+
)
|
184 |
search_content = f"Here are some search results related to your question:\n\n{search_content}\n\n"
|
185 |
else:
|
186 |
search_content = "Sorry, no relevant search results found.\n\n"
|
|
|
206 |
# Hugging Face API์ ์ฟผ๋ฆฌ ์ ์ก
|
207 |
output = query(payload, api_url)
|
208 |
|
209 |
+
# API ์๋ต ์ฒ๋ฆฌ
|
210 |
if output is not None and isinstance(output, list) and len(output) > 0:
|
211 |
if 'generated_text' in output[0]:
|
212 |
assistant_response = output[0]['generated_text'].strip()
|
213 |
+
# ๋ด๋ถ ์ฒด์ธ ์ค๋ธ ์ ์ ๊ฑฐ
|
214 |
+
unique_response = remove_chain_of_thought(assistant_response)
|
|
|
|
|
215 |
|
216 |
logger.info(f"Generated response: {unique_response}")
|
217 |
|