my-chatbot / app.py
abhi1nandy2's picture
Update app.py
b4b7a21 verified
raw
history blame
2.09 kB
import gradio as gr
import requests
from bs4 import BeautifulSoup, Comment
from llama_cpp import Llama
# Function to extract visible text from a webpage
def tag_visible(element):
if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
return False
if isinstance(element, Comment):
return False
return True
def get_text_from_url(url):
response = requests.get(url, timeout=10)
soup = BeautifulSoup(response.text, 'html.parser')
texts = soup.find_all(text=True)
visible_texts = filter(tag_visible, texts)
return " ".join(t.strip() for t in visible_texts)
# Pre-fetch and truncate homepage text
text_list = []
homepage_url = "https://sites.google.com/view/abhilashnandy/home/"
extensions = ["", "pmrf-profile-page"]
for ext in extensions:
try:
full_text = get_text_from_url(homepage_url + ext)
truncated_text = full_text[:2000] # Using first 2000 characters for more context
text_list.append(truncated_text)
except Exception as e:
text_list.append(f"Error fetching {homepage_url+ext}: {str(e)}")
CONTEXT = " ".join(text_list)
# Load the Mistral model (low-latency, CPU optimized)
llm = Llama(model_path="mistral-7b-instruct-v0.1.Q4_K_M.gguf", n_ctx=4096, n_threads=6, verbose=False)
# Function to answer queries
def answer_query(query):
prompt = (
"You are an AI chatbot answering queries based on the homepage of Abhilash Nandy. "
"Your responses should be concise (under 30 words) and directly relevant to the provided context.\n\n"
f"Context: {CONTEXT}\n\nUser: {query}\nAI:"
)
response = llm(prompt, max_tokens=50, stop=["\nUser:", "\nAI:"], echo=False)
return response["choices"][0]["text"].strip()
# Gradio Interface
iface = gr.Interface(
fn=answer_query,
inputs=gr.Textbox(lines=2, placeholder="Ask a question about Abhilash Nandy..."),
outputs="text",
title="Homepage QA Chatbot",
description="Ask me anything about Abhilash Nandy's homepage."
)
if __name__ == '__main__':
iface.launch()