Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from bs4 import BeautifulSoup | |
| import requests | |
| from groq import Groq | |
| import os | |
| from dotenv import load_env | |
| class Website: | |
| def __init__(self, url): | |
| """ | |
| Create this Website object from the given url using the BeautifulSoup library | |
| """ | |
| self.url = url | |
| response = requests.get(url) | |
| soup = BeautifulSoup(response.content, 'html.parser') | |
| self.title = soup.title.string if soup.title else "No title found" | |
| for irrelevant in soup.body(["script", "style", "img", "input"]): | |
| irrelevant.decompose() | |
| self.text = soup.body.get_text(separator="\n", strip=True) | |
| # Initialize Groq client | |
| api_key = os.getenv('GROQ_API_KEY') | |
| client = Groq(api_key=api_key) | |
| # Streamlit UI | |
| st.title("Welcome to WebBot") | |
| st.write("Enter a website URL and ask questions about its content!") | |
| # Input fields | |
| url = st.text_input("Website URL:", "https://example.com or .ac.ke " ) | |
| user_query = st.text_area("What would you like to know about this website") | |
| if st.button("Submit"): | |
| # Scrape website content | |
| with st.spinner("Scraping website..."): | |
| website = Website(url) | |
| if "Error" in website.title: | |
| st.error("Failed to load the website. Please check the URL.") | |
| else: | |
| st.success("Website loaded successfully!") | |
| st.write(f"**Website Title:** {website.title}") | |
| # Call Groq API for processing | |
| st.write("Querying the website...") | |
| with st.spinner("Processing your query..."): | |
| try: | |
| chat_streaming = client.chat.completions.create( | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful assistant specializing in extracting and analyzing website content. Answer questions based on the provided website's content. Ensure responses are clear, concise, and formatted in Markdown for better readability."}, | |
| {"role": "user", "content": f"{user_query} Here's the content:\n{website.text}"} | |
| ], | |
| model="llama3-groq-70b-8192-tool-use-preview", | |
| temperature=0.3, | |
| max_tokens=1200, | |
| top_p=1, | |
| stream=True, | |
| ) | |
| st.write('Passed model') | |
| except Exception as e: | |
| st.error(f"Failed to process query: {e}") | |
| response = "" | |
| try: | |
| for chunk in chat_streaming: | |
| content = chunk.choices[0].delta.content | |
| if content: # Ensure content is not None | |
| response += content | |
| st.write("### Response:") | |
| st.write(response) | |
| except Exception as e: | |
| st.error(f"Failed to process query: {e}") |