Macdensten91 commited on
Commit
f0915ac
·
verified ·
1 Parent(s): f035190

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -63
app.py CHANGED
@@ -1,101 +1,73 @@
1
- import re
2
- import time
3
  import random
4
- import gradio as gr
5
  from huggingface_hub import InferenceClient
6
 
7
- # Optional: Enable scraping if your site is deployed.
8
- ENABLE_SCRAPING = False
9
- SITE_URL = "https://your-agri-future-site.com"
10
-
11
- # Global variable to hold scraped content
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
- def scrape_site(url):
22
- options = Options()
23
- options.headless = True # Run browser in headless mode.
24
- driver = webdriver.Chrome(options=options)
25
- driver.get(url)
26
- # Use explicit waits in production; here we use a basic sleep.
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
- greetings = {
48
- "en": ["hello", "hi", "hey", "good morning", "good afternoon", "good evening"],
49
- "fr": ["bonjour", "salut", "coucou", "bonsoir"],
50
- "am": ["ሰላም", "ሰላም እንደምን", "እንዴት"]
51
- }
52
  greet_list = greetings.get(lang, greetings["en"])
53
- # For languages using Latin script, convert to lower case.
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
- # Define language-specific system prompts for generating a dynamic out-of-scope message.
 
 
 
61
  system_prompts = {
62
  "en": (
63
  "You are a helpful chatbot specializing in agriculture and agro-investment. "
64
- "A user just asked a question that is not related to these topics. "
65
- "Generate a friendly, varied, and intelligent out-of-scope response in English that kindly encourages the user to ask about agriculture or agro-investment."
66
  ),
67
  "fr": (
68
  "Vous êtes un chatbot utile spécialisé dans l'agriculture et les investissements agroalimentaires. "
69
- "Un utilisateur vient de poser une question qui ne concerne pas ces sujets. "
70
- "Générez une réponse élégante, variée et intelligente en français pour indiquer que la question est hors de portée, en invitant l'utilisateur à poser une question sur l'agriculture ou les investissements agroalimentaires."
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 without streaming to generate the complete message.
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
- # Depending on the client structure, adjust the extraction of the generated text.
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 retrieve_relevant_snippet(query: str, text: str, max_length: int = 300) -> str:
107
- sentences = re.split(r'[.?!]', text)
108
- for sentence in sentences
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)