Update app.py
Browse filesshifted to qwant API search
app.py
CHANGED
@@ -7,54 +7,60 @@ client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
|
|
7 |
|
8 |
def translate_to_english(text: str) -> str:
|
9 |
try:
|
10 |
-
# Translate the text to English.
|
11 |
return GoogleTranslator(source='auto', target='en').translate(text)
|
12 |
except Exception:
|
13 |
return text
|
14 |
|
15 |
def translate_to_bisaya(text: str) -> str:
|
16 |
try:
|
17 |
-
# 'ceb' is the ISO code for Cebuano/Bisaya.
|
18 |
return GoogleTranslator(source='auto', target='ceb').translate(text)
|
19 |
except Exception:
|
20 |
return text
|
21 |
|
22 |
def get_internet_data(query: str) -> str:
|
23 |
-
|
|
|
|
|
|
|
24 |
params = {
|
25 |
"q": query,
|
26 |
-
"
|
27 |
-
"
|
28 |
-
"
|
|
|
|
|
|
|
29 |
}
|
30 |
try:
|
31 |
response = requests.get(url, params=params, timeout=5)
|
32 |
response.raise_for_status()
|
33 |
data = response.json()
|
34 |
-
|
35 |
-
if
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
except Exception:
|
38 |
-
|
39 |
-
return
|
40 |
|
41 |
def respond(message, history: list[tuple[str, str]]):
|
42 |
# Step 1: Translate the query from Bisaya to English.
|
43 |
english_query = translate_to_english(message)
|
44 |
|
45 |
-
# Step 2: Search the web using the translated query.
|
46 |
search_result = get_internet_data(english_query)
|
47 |
|
48 |
-
# Step 3: Translate the search result to Bisaya.
|
49 |
bisaya_search_result = translate_to_bisaya(search_result)
|
50 |
|
51 |
-
# Enrich the original query with the
|
52 |
enriched_message = (
|
53 |
-
f"{message}\n\nMga resulta gikan sa internet (isinalin sa bisaya): "
|
54 |
-
f"{bisaya_search_result}"
|
55 |
)
|
56 |
|
57 |
-
# Build the conversation messages.
|
58 |
system_message = (
|
59 |
"Ikaw usa ka buotan nga Chatbot. Tubaga lang sa binisaya. "
|
60 |
"Gamiton ang bag-ong kasayuran nga nakuha gikan sa internet. "
|
@@ -63,7 +69,7 @@ def respond(message, history: list[tuple[str, str]]):
|
|
63 |
max_tokens = 4096
|
64 |
temperature = 0.6
|
65 |
top_p = 0.95
|
66 |
-
|
67 |
messages = [{"role": "system", "content": system_message}]
|
68 |
for user_text, assistant_text in history:
|
69 |
if user_text:
|
@@ -72,7 +78,7 @@ def respond(message, history: list[tuple[str, str]]):
|
|
72 |
messages.append({"role": "assistant", "content": assistant_text})
|
73 |
messages.append({"role": "user", "content": enriched_message})
|
74 |
|
75 |
-
#
|
76 |
full_response = ""
|
77 |
for token_message in client.chat_completion(
|
78 |
messages,
|
@@ -87,8 +93,8 @@ def respond(message, history: list[tuple[str, str]]):
|
|
87 |
full_response += token
|
88 |
if len(full_response) > 3000:
|
89 |
break
|
90 |
-
|
91 |
-
#
|
92 |
final_response = translate_to_bisaya(full_response)
|
93 |
yield final_response
|
94 |
|
|
|
7 |
|
8 |
def translate_to_english(text: str) -> str:
|
9 |
try:
|
|
|
10 |
return GoogleTranslator(source='auto', target='en').translate(text)
|
11 |
except Exception:
|
12 |
return text
|
13 |
|
14 |
def translate_to_bisaya(text: str) -> str:
|
15 |
try:
|
|
|
16 |
return GoogleTranslator(source='auto', target='ceb').translate(text)
|
17 |
except Exception:
|
18 |
return text
|
19 |
|
20 |
def get_internet_data(query: str) -> str:
|
21 |
+
"""
|
22 |
+
Uses Qwant's free search API to fetch a snippet based on the query.
|
23 |
+
"""
|
24 |
+
url = "https://api.qwant.com/v3/search/web"
|
25 |
params = {
|
26 |
"q": query,
|
27 |
+
"count": 10,
|
28 |
+
"offset": 0,
|
29 |
+
"t": "web",
|
30 |
+
"safesearch": 1,
|
31 |
+
"locale": "en_US",
|
32 |
+
"uiv": 4,
|
33 |
}
|
34 |
try:
|
35 |
response = requests.get(url, params=params, timeout=5)
|
36 |
response.raise_for_status()
|
37 |
data = response.json()
|
38 |
+
items = data.get("data", {}).get("result", {}).get("items", [])
|
39 |
+
if items:
|
40 |
+
snippet = items[0].get("desc", "")
|
41 |
+
if not snippet:
|
42 |
+
snippet = items[0].get("title", "")
|
43 |
+
else:
|
44 |
+
snippet = "Wala koy nakuha nga impormasyon gikan sa Qwant search."
|
45 |
except Exception:
|
46 |
+
snippet = "Naay problema sa pagkuha sa impormasyon gikan sa Qwant search."
|
47 |
+
return snippet
|
48 |
|
49 |
def respond(message, history: list[tuple[str, str]]):
|
50 |
# Step 1: Translate the query from Bisaya to English.
|
51 |
english_query = translate_to_english(message)
|
52 |
|
53 |
+
# Step 2: Search the web using Qwant's API with the translated query.
|
54 |
search_result = get_internet_data(english_query)
|
55 |
|
56 |
+
# Step 3: Translate the search result back to Bisaya.
|
57 |
bisaya_search_result = translate_to_bisaya(search_result)
|
58 |
|
59 |
+
# Enrich the original query with the translated search result.
|
60 |
enriched_message = (
|
61 |
+
f"{message}\n\nMga resulta gikan sa internet (isinalin sa bisaya): {bisaya_search_result}"
|
|
|
62 |
)
|
63 |
|
|
|
64 |
system_message = (
|
65 |
"Ikaw usa ka buotan nga Chatbot. Tubaga lang sa binisaya. "
|
66 |
"Gamiton ang bag-ong kasayuran nga nakuha gikan sa internet. "
|
|
|
69 |
max_tokens = 4096
|
70 |
temperature = 0.6
|
71 |
top_p = 0.95
|
72 |
+
|
73 |
messages = [{"role": "system", "content": system_message}]
|
74 |
for user_text, assistant_text in history:
|
75 |
if user_text:
|
|
|
78 |
messages.append({"role": "assistant", "content": assistant_text})
|
79 |
messages.append({"role": "user", "content": enriched_message})
|
80 |
|
81 |
+
# Get the complete response from the model.
|
82 |
full_response = ""
|
83 |
for token_message in client.chat_completion(
|
84 |
messages,
|
|
|
93 |
full_response += token
|
94 |
if len(full_response) > 3000:
|
95 |
break
|
96 |
+
|
97 |
+
# Translate the final response to Bisaya.
|
98 |
final_response = translate_to_bisaya(full_response)
|
99 |
yield final_response
|
100 |
|