web-query / app.py
sarthakrw's picture
Update app.py
4a318e0
from ctransformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
model_path_or_repo_id="TheBloke/Llama-2-7B-chat-GGML",
max_new_tokens=512,
temperature=0.6,
top_p=0.95,
repetition_penalty=1.15
)
system_message = """
You are a helpful, respectful and honest assistant. Your job is to answer the users query as best as possible given the Web Page Content. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. If you DO NOT KNOW THE ANSWER DO NOT SHARE FALSE INFORMATION.
You have been given scraped text content of a webpage under the section called "Web Page Content". Using this information answer the users query. However, if the webpage DOES NOT contain the answer to the query, you CAN answer based on your existing knowledge IF you are sure of the answer, but ALWAYS let the user know when doing so.
"""
def generate_prompt(system_message, context, prompt):
prompt=f'''[INST] <<SYS>>
{system_message}
<</SYS>>
Web Page Content:
```
{context}
```
{prompt} [/INST]'''
return prompt
import requests
from bs4 import BeautifulSoup
import re
def scraper(url):
req = requests.get(url)
soup = BeautifulSoup(req.content, "html.parser")
context = soup.get_text()
relevant_text = soup.get_text()
cleaned_text = re.sub(r'\s+', ' ', relevant_text).strip()
return cleaned_text
def run(url, input):
context = scraper(url)
response = model(generate_prompt(system_message=system_message, context=context, prompt=input))
return response
import gradio as gr
# Create a Gradio interface
iface = gr.Interface(
fn=run,
inputs=["text","text"],
outputs="text",
title="Web Query App",
description="Enter the webpage url and your query\nIMPORTANT: Larger webpages are likely to cause error due to lack of computational resources"
)
# Launch the interface
iface.launch(inline=False)