Update app.py
Browse files
app.py
CHANGED
@@ -1,101 +1,73 @@
|
|
1 |
-
import re
|
2 |
-
import time
|
3 |
import random
|
4 |
-
import
|
5 |
from huggingface_hub import InferenceClient
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
knowledge_base = ""
|
13 |
-
|
14 |
-
# --- Optional: Scraping Functionality ---
|
15 |
-
if ENABLE_SCRAPING:
|
16 |
-
try:
|
17 |
-
from selenium import webdriver
|
18 |
-
from selenium.webdriver.chrome.options import Options
|
19 |
-
from selenium.webdriver.common.by import By
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
time.sleep(5)
|
28 |
-
try:
|
29 |
-
# Customize the selector based on your site's HTML structure.
|
30 |
-
content_element = driver.find_element(By.ID, "content")
|
31 |
-
page_text = content_element.text
|
32 |
-
except Exception as e:
|
33 |
-
page_text = "Error encountered during scraping: " + str(e)
|
34 |
-
driver.quit()
|
35 |
-
return page_text
|
36 |
-
|
37 |
-
knowledge_base = scrape_site(SITE_URL)
|
38 |
-
print("Scraped knowledge base successfully.")
|
39 |
-
except Exception as e:
|
40 |
-
print("Scraping failed or Selenium is not configured:", e)
|
41 |
-
else:
|
42 |
-
print("Scraping is disabled; proceeding without scraped site content.")
|
43 |
-
|
44 |
-
# --- Multilingual Helpers ---
|
45 |
|
46 |
def is_greeting(query: str, lang: str) -> bool:
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
"am": ["ሰላም", "ሰላም እንደምን", "እንዴት"]
|
51 |
-
}
|
52 |
greet_list = greetings.get(lang, greetings["en"])
|
53 |
-
#
|
54 |
if lang != "am":
|
55 |
query = query.lower()
|
56 |
return any(query.startswith(greet) for greet in greet_list)
|
57 |
|
58 |
-
# Rather than using fixed out-of-scope messages, use the model via Hugging Face to generate them.
|
59 |
def generate_dynamic_out_of_scope_message(language: str) -> str:
|
60 |
-
|
|
|
|
|
|
|
61 |
system_prompts = {
|
62 |
"en": (
|
63 |
"You are a helpful chatbot specializing in agriculture and agro-investment. "
|
64 |
-
"A user
|
65 |
-
"Generate a friendly
|
66 |
),
|
67 |
"fr": (
|
68 |
"Vous êtes un chatbot utile spécialisé dans l'agriculture et les investissements agroalimentaires. "
|
69 |
-
"Un utilisateur
|
70 |
-
"Générez une réponse
|
71 |
),
|
72 |
"am": (
|
73 |
-
"እርስዎ በግብርናና በአገልግሎት ስርዓተ-ቢዝነስ ውስጥ
|
74 |
-
"ተጠቃሚው
|
75 |
-
"በአማርኛ
|
76 |
)
|
77 |
}
|
78 |
prompt = system_prompts.get(language, system_prompts["en"])
|
79 |
messages = [{"role": "system", "content": prompt}]
|
80 |
|
81 |
-
# Call the model
|
82 |
response = client.chat_completion(
|
83 |
messages,
|
84 |
max_tokens=80,
|
85 |
-
stream=False,
|
86 |
temperature=0.7,
|
87 |
top_p=0.95,
|
88 |
)
|
89 |
-
#
|
90 |
try:
|
91 |
out_message = response.choices[0].message.content
|
92 |
except AttributeError:
|
93 |
-
# If the response structure differs, do a fallback conversion.
|
94 |
out_message = str(response)
|
95 |
return out_message.strip()
|
96 |
|
97 |
-
# A helper to determine domain relevance (basic implementation; can be expanded).
|
98 |
def is_domain_query(query: str) -> bool:
|
|
|
|
|
|
|
99 |
domain_keywords = [
|
100 |
"agriculture", "farming", "crop", "agro", "investment", "soil",
|
101 |
"irrigation", "harvest", "organic", "sustainable", "agribusiness",
|
@@ -103,6 +75,19 @@ def is_domain_query(query: str) -> bool:
|
|
103 |
]
|
104 |
return any(re.search(r"\b" + keyword + r"\b", query, re.IGNORECASE) for keyword in domain_keywords)
|
105 |
|
106 |
-
def
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import random
|
2 |
+
import re
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
5 |
+
# Initialize the InferenceClient with your Hugging Face API token
|
6 |
+
client = InferenceClient(
|
7 |
+
model="HuggingFaceH4/zephyr-7b-beta", # Specify your model here
|
8 |
+
token="your_huggingface_api_token" # Replace with your actual token
|
9 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Multilingual greetings dictionary
|
12 |
+
greetings = {
|
13 |
+
"en": ["hello", "hi", "hey", "good morning", "good afternoon", "good evening"],
|
14 |
+
"fr": ["bonjour", "salut", "coucou", "bonsoir"],
|
15 |
+
"am": ["ሰላም", "ሰላም እንደምን", "እንዴት"]
|
16 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
def is_greeting(query: str, lang: str) -> bool:
|
19 |
+
"""
|
20 |
+
Check if the user's query is a greeting in the specified language.
|
21 |
+
"""
|
|
|
|
|
22 |
greet_list = greetings.get(lang, greetings["en"])
|
23 |
+
# Convert to lowercase for non-Amharic languages
|
24 |
if lang != "am":
|
25 |
query = query.lower()
|
26 |
return any(query.startswith(greet) for greet in greet_list)
|
27 |
|
|
|
28 |
def generate_dynamic_out_of_scope_message(language: str) -> str:
|
29 |
+
"""
|
30 |
+
Generate a dynamic out-of-scope message using the Hugging Face Inference API.
|
31 |
+
"""
|
32 |
+
# Define language-specific system prompts
|
33 |
system_prompts = {
|
34 |
"en": (
|
35 |
"You are a helpful chatbot specializing in agriculture and agro-investment. "
|
36 |
+
"A user has asked a question unrelated to these topics. "
|
37 |
+
"Generate a friendly and intelligent out-of-scope response in English, encouraging the user to ask about agriculture or agro-investment."
|
38 |
),
|
39 |
"fr": (
|
40 |
"Vous êtes un chatbot utile spécialisé dans l'agriculture et les investissements agroalimentaires. "
|
41 |
+
"Un utilisateur a posé une question sans rapport avec ces sujets. "
|
42 |
+
"Générez une réponse amicale et intelligente en français, encourageant l'utilisateur à poser des questions sur l'agriculture ou les investissements agroalimentaires."
|
43 |
),
|
44 |
"am": (
|
45 |
+
"እርስዎ በግብርናና በአገልግሎት ስርዓተ-ቢዝነስ ውስጥ የሚሰራ እገዛ የሚሰጥ ቻትቦት ነው። "
|
46 |
+
"ተጠቃሚው ከእነዚህ ጉዳዮች ውጪ ጥያቄ አቀርቧል። "
|
47 |
+
"በአማርኛ የተሰጠ የውጭ ክፍል ምላሽ ይፍጠሩ፣ ተጠቃሚውን ለግብርና ወይም ለአገልግሎት ስርዓተ-ቢዝነስ ጥያቄዎች ለመጠየቅ ያበረታታ።"
|
48 |
)
|
49 |
}
|
50 |
prompt = system_prompts.get(language, system_prompts["en"])
|
51 |
messages = [{"role": "system", "content": prompt}]
|
52 |
|
53 |
+
# Call the model to generate the response
|
54 |
response = client.chat_completion(
|
55 |
messages,
|
56 |
max_tokens=80,
|
|
|
57 |
temperature=0.7,
|
58 |
top_p=0.95,
|
59 |
)
|
60 |
+
# Extract the generated message content
|
61 |
try:
|
62 |
out_message = response.choices[0].message.content
|
63 |
except AttributeError:
|
|
|
64 |
out_message = str(response)
|
65 |
return out_message.strip()
|
66 |
|
|
|
67 |
def is_domain_query(query: str) -> bool:
|
68 |
+
"""
|
69 |
+
Determine if the query is related to agriculture or agro-investment.
|
70 |
+
"""
|
71 |
domain_keywords = [
|
72 |
"agriculture", "farming", "crop", "agro", "investment", "soil",
|
73 |
"irrigation", "harvest", "organic", "sustainable", "agribusiness",
|
|
|
75 |
]
|
76 |
return any(re.search(r"\b" + keyword + r"\b", query, re.IGNORECASE) for keyword in domain_keywords)
|
77 |
|
78 |
+
def handle_user_query(query: str, lang: str = "en") -> str:
|
79 |
+
"""
|
80 |
+
Process the user's query and provide an appropriate response.
|
81 |
+
"""
|
82 |
+
if is_greeting(query, lang):
|
83 |
+
return random.choice(greetings.get(lang, greetings["en"])).capitalize() + "!"
|
84 |
+
elif is_domain_query(query):
|
85 |
+
# Here you would integrate your domain-specific response generation
|
86 |
+
return "This is a domain-specific question. Processing accordingly..."
|
87 |
+
else:
|
88 |
+
return generate_dynamic_out_of_scope_message(lang)
|
89 |
+
|
90 |
+
# Example usage
|
91 |
+
user_query = "Tell me about space travel."
|
92 |
+
response = handle_user_query(user_query, lang="en")
|
93 |
+
print(response)
|