Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import requests | |
| from bs4 import BeautifulSoup | |
| import re | |
| # Initialize Hugging Face InferenceClient | |
| client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta") | |
| def is_url(input_string): | |
| url_pattern = re.compile(r'^(https?://)?([a-zA-Z0-9.-]+(?:\.[a-zA-Z]{2,4}))(?:/[^\s]*)?/?$') | |
| return url_pattern.match(input_string) is not None | |
| def scrape_website(url): | |
| try: | |
| response = requests.get(url) | |
| soup = BeautifulSoup(response.text, 'html.parser') | |
| return ' '.join([p.text for p in soup.find_all('p')]) | |
| except Exception as e: | |
| return f"Failed to scrape the website. Error: {str(e)}" | |
| def generate_response(input_text): | |
| if is_url(input_text): | |
| scraped_content = scrape_website(input_text) | |
| return f"I've found the following content from the website:\n\n{scraped_content}" | |
| else: | |
| response = client.text_generation(f"User: {input_text}\nAssistant:", max_new_tokens=100, temperature=0.6) | |
| return response['generated_text'] | |
| def process_input(input_text): | |
| return generate_response(input_text) | |
| # Define Gradio interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Conversational Chatbot with Web Scraping Ability") | |
| with gr.Row(): | |
| with gr.Column(): | |
| text_input = gr.Textbox(label="Enter your message or URL here") | |
| submit_button = gr.Button("Submit") | |
| with gr.Column(): | |
| response_output = gr.Textbox(label="Chatbot Response", interactive=False) | |
| submit_button.click(fn=process_input, inputs=[text_input], outputs=[response_output]) | |
| demo.launch() | |