File size: 3,396 Bytes
1915306 0e9eccb 72e07db 1915306 777bb52 1915306 2723984 0e9eccb faf8c49 0e9eccb faf8c49 0e9eccb faf8c49 4d0e77c faf8c49 0e9eccb 8b5f3bd 2723984 faf8c49 2723984 faf8c49 2723984 faf8c49 2723984 faf8c49 2723984 3a9a6ab 2723984 3a9a6ab 8b5f3bd 4a65c44 8b5f3bd faf8c49 8b5f3bd e8b0ec8 2e6435e 2723984 faf8c49 39432c2 987b836 1915306 987b836 39432c2 0c08de9 faf8c49 39432c2 8b5f3bd 1915306 |
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 |
import gradio as gr
from huggingface_hub import InferenceClient
import requests
from deep_translator import GoogleTranslator
client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
def translate_to_english(text: str) -> str:
try:
return GoogleTranslator(source='auto', target='en').translate(text)
except Exception:
return text
def translate_to_bisaya(text: str) -> str:
try:
return GoogleTranslator(source='auto', target='ceb').translate(text)
except Exception:
return text
def get_internet_data(query: str) -> str:
"""
Uses Qwant's free search API to fetch a snippet based on the query.
"""
url = "https://api.qwant.com/v3/search/web"
params = {
"q": query,
"count": 10,
"offset": 0,
"t": "web",
"safesearch": 1,
"locale": "en_US",
"uiv": 4,
}
try:
response = requests.get(url, params=params, timeout=5)
response.raise_for_status()
data = response.json()
items = data.get("data", {}).get("result", {}).get("items", [])
if items:
snippet = items[0].get("desc", "")
if not snippet:
snippet = items[0].get("title", "")
else:
snippet = "Wala koy nakuha nga impormasyon gikan sa Qwant search."
except Exception:
snippet = "Naay problema sa pagkuha sa impormasyon gikan sa Qwant search."
return snippet
def respond(message, history: list[tuple[str, str]]):
# Step 1: Translate the query from Bisaya to English.
english_query = translate_to_english(message)
# Step 2: Search the web using Qwant's API with the translated query.
search_result = get_internet_data(english_query)
# Step 3: Translate the search result back to Bisaya.
bisaya_search_result = translate_to_bisaya(search_result)
# Enrich the original query with the translated search result.
enriched_message = (
f"{message}\n\nMga resulta gikan sa internet (isinalin sa bisaya): {bisaya_search_result}"
)
system_message = (
"Ikaw usa ka buotan nga Chatbot. Tubaga lang sa binisaya. "
"Gamiton ang bag-ong kasayuran nga nakuha gikan sa internet. "
"Ayaw og gamit ug English nga pinulungan."
)
max_tokens = 4096
temperature = 0.6
top_p = 0.95
messages = [{"role": "system", "content": system_message}]
for user_text, assistant_text in history:
if user_text:
messages.append({"role": "user", "content": user_text})
if assistant_text:
messages.append({"role": "assistant", "content": assistant_text})
messages.append({"role": "user", "content": enriched_message})
# Get the complete response from the model.
full_response = ""
for token_message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = token_message.choices[0].delta.get("content", "")
if not token:
break
full_response += token
if len(full_response) > 3000:
break
# Translate the final response to Bisaya.
final_response = translate_to_bisaya(full_response)
yield final_response
demo = gr.ChatInterface(respond)
if __name__ == "__main__":
demo.launch()
|