Spaces:
Runtime error
Runtime error
File size: 2,236 Bytes
bbd5c76 06323bb 3421ed4 6761a81 3421ed4 e921d5b 06323bb 8735569 06323bb 717d3b5 e921d5b 717d3b5 bbd5c76 3421ed4 717d3b5 3421ed4 bbd5c76 b9fcfca bbd5c76 b9fcfca |
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 |
import gradio as gr
from huggingface_hub import InferenceClient
import requests
from bs4 import BeautifulSoup
from bs4.element import Comment
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)
soup = BeautifulSoup(response.text, 'html.parser')
texts = soup.find_all(text=True)
visible_texts = filter(tag_visible, texts)
return "\n".join(t.strip() for t in visible_texts)
# Pre-fetch and truncate homepage text to reduce the prompt length.
text_list = []
homepage_url = "https://sites.google.com/view/abhilashnandy/home/"
extensions = ["", "pmrf-profile-page"]
for ext in extensions:
full_text = get_text_from_url(homepage_url + ext)
truncated_text = full_text[:1000] # using only the first 1000 characters per extension
text_list.append(truncated_text)
SYSTEM_MESSAGE = (
"You are a QA chatbot to answer queries (in less than 30 words) on my homepage. "
"Context: " + " ".join(text_list)
)
# Create a Hugging Face Inference client using a CPU-friendly model.
# Here we use 'google/flan-t5-base' as an example; you can adjust the model if needed.
client = InferenceClient(model="google/flan-t5-base")
def answer_query(query):
# Compose a prompt using the system message, user query, and a reminder for a short answer.
prompt = SYSTEM_MESSAGE + "\nUser: " + query + "\nAnswer in less than 30 words:"
# Generate answer with a limit on new tokens to ensure brevity.
result = client.text_generation(prompt, max_new_tokens=60)
# Handle both list or direct string responses from the inference client.
if isinstance(result, list):
answer = result[0].get("generated_text", "")
else:
answer = result
return answer.strip()
iface = gr.Interface(
fn=answer_query,
inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
outputs="text",
title="Homepage QA Chatbot",
description="A chatbot answering queries about the homepage using pre-fetched context."
)
if __name__ == '__main__':
iface.launch() |