abhi1nandy2 commited on
Commit
b4b7a21
·
verified ·
1 Parent(s): 4c87fad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
  import requests
4
- from bs4 import BeautifulSoup
5
- from bs4.element import Comment
6
 
 
7
  def tag_visible(element):
8
  if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
9
  return False
@@ -12,48 +12,49 @@ def tag_visible(element):
12
  return True
13
 
14
  def get_text_from_url(url):
15
- response = requests.get(url)
16
  soup = BeautifulSoup(response.text, 'html.parser')
17
  texts = soup.find_all(text=True)
18
  visible_texts = filter(tag_visible, texts)
19
- return "\n".join(t.strip() for t in visible_texts)
20
 
21
- # Pre-fetch and truncate homepage text to reduce the prompt length.
22
  text_list = []
23
  homepage_url = "https://sites.google.com/view/abhilashnandy/home/"
24
  extensions = ["", "pmrf-profile-page"]
 
25
  for ext in extensions:
26
- full_text = get_text_from_url(homepage_url + ext)
27
- truncated_text = full_text[:1000] # using only the first 1000 characters per extension
28
- text_list.append(truncated_text)
 
 
 
29
 
30
- SYSTEM_MESSAGE = (
31
- "You are a QA chatbot to answer queries (in less than 30 words) on my homepage. "
32
- "Context: " + " ".join(text_list)
33
- )
34
 
35
- # Create a Hugging Face Inference client using a CPU-friendly model.
36
- # Here we use 'google/flan-t5-base' as an example; you can adjust the model if needed.
37
- client = InferenceClient(model="google/flan-t5-base")
38
 
 
39
  def answer_query(query):
40
- # Compose a prompt using the system message, user query, and a reminder for a short answer.
41
- prompt = SYSTEM_MESSAGE + "\nUser: " + query + "\nAnswer in less than 30 words:"
42
- # Generate answer with a limit on new tokens to ensure brevity.
43
- result = client.text_generation(prompt, max_new_tokens=60)
44
- # Handle both list or direct string responses from the inference client.
45
- if isinstance(result, list):
46
- answer = result[0].get("generated_text", "")
47
- else:
48
- answer = result
49
- return answer.strip()
50
-
51
  iface = gr.Interface(
52
  fn=answer_query,
53
- inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
54
  outputs="text",
55
  title="Homepage QA Chatbot",
56
- description="A chatbot answering queries about the homepage using pre-fetched context."
57
  )
58
 
59
  if __name__ == '__main__':
 
1
  import gradio as gr
 
2
  import requests
3
+ from bs4 import BeautifulSoup, Comment
4
+ from llama_cpp import Llama
5
 
6
+ # Function to extract visible text from a webpage
7
  def tag_visible(element):
8
  if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
9
  return False
 
12
  return True
13
 
14
  def get_text_from_url(url):
15
+ response = requests.get(url, timeout=10)
16
  soup = BeautifulSoup(response.text, 'html.parser')
17
  texts = soup.find_all(text=True)
18
  visible_texts = filter(tag_visible, texts)
19
+ return " ".join(t.strip() for t in visible_texts)
20
 
21
+ # Pre-fetch and truncate homepage text
22
  text_list = []
23
  homepage_url = "https://sites.google.com/view/abhilashnandy/home/"
24
  extensions = ["", "pmrf-profile-page"]
25
+
26
  for ext in extensions:
27
+ try:
28
+ full_text = get_text_from_url(homepage_url + ext)
29
+ truncated_text = full_text[:2000] # Using first 2000 characters for more context
30
+ text_list.append(truncated_text)
31
+ except Exception as e:
32
+ text_list.append(f"Error fetching {homepage_url+ext}: {str(e)}")
33
 
34
+ CONTEXT = " ".join(text_list)
 
 
 
35
 
36
+ # Load the Mistral model (low-latency, CPU optimized)
37
+ llm = Llama(model_path="mistral-7b-instruct-v0.1.Q4_K_M.gguf", n_ctx=4096, n_threads=6, verbose=False)
 
38
 
39
+ # Function to answer queries
40
  def answer_query(query):
41
+ prompt = (
42
+ "You are an AI chatbot answering queries based on the homepage of Abhilash Nandy. "
43
+ "Your responses should be concise (under 30 words) and directly relevant to the provided context.\n\n"
44
+ f"Context: {CONTEXT}\n\nUser: {query}\nAI:"
45
+ )
46
+
47
+ response = llm(prompt, max_tokens=50, stop=["\nUser:", "\nAI:"], echo=False)
48
+
49
+ return response["choices"][0]["text"].strip()
50
+
51
+ # Gradio Interface
52
  iface = gr.Interface(
53
  fn=answer_query,
54
+ inputs=gr.Textbox(lines=2, placeholder="Ask a question about Abhilash Nandy..."),
55
  outputs="text",
56
  title="Homepage QA Chatbot",
57
+ description="Ask me anything about Abhilash Nandy's homepage."
58
  )
59
 
60
  if __name__ == '__main__':