Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import trafilatura
|
| 5 |
+
from smolagents import Agent
|
| 6 |
+
|
| 7 |
+
# Streamlit UI
|
| 8 |
+
def main():
|
| 9 |
+
st.set_page_config(page_title="AI Documentation Assistant", layout="wide")
|
| 10 |
+
st.title("π AI Documentation Assistant")
|
| 11 |
+
|
| 12 |
+
st.write("Enter the top-level URL of your documentation, and I'll find the most relevant article to answer your question.")
|
| 13 |
+
|
| 14 |
+
# User input
|
| 15 |
+
doc_url = st.text_input("π Documentation URL (Homepage)", "https://example.com/docs")
|
| 16 |
+
user_question = st.text_area("β Your Question", "How do I reset my password?")
|
| 17 |
+
|
| 18 |
+
if st.button("π Find Answer"):
|
| 19 |
+
with st.spinner("Searching for relevant information..."):
|
| 20 |
+
article_url, extracted_text = find_relevant_article(doc_url, user_question)
|
| 21 |
+
if article_url:
|
| 22 |
+
answer = generate_answer(user_question, extracted_text)
|
| 23 |
+
|
| 24 |
+
st.success("β
Answer Found!")
|
| 25 |
+
st.write(answer)
|
| 26 |
+
st.write(f"[π Read Full Article]({article_url})")
|
| 27 |
+
else:
|
| 28 |
+
st.error("β οΈ No relevant articles found.")
|
| 29 |
+
|
| 30 |
+
# Step 3 & 4: Crawling and Finding the Most Relevant Article
|
| 31 |
+
def find_relevant_article(base_url, question):
|
| 32 |
+
"""Crawls the top-domain docs, finds the most relevant article, and extracts text."""
|
| 33 |
+
response = requests.get(base_url)
|
| 34 |
+
if response.status_code != 200:
|
| 35 |
+
return None, None
|
| 36 |
+
|
| 37 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 38 |
+
links = [a['href'] for a in soup.find_all('a', href=True) if base_url in a['href']]
|
| 39 |
+
|
| 40 |
+
best_match = None
|
| 41 |
+
best_text = ""
|
| 42 |
+
|
| 43 |
+
for link in links[:10]: # Limit to first 10 links for now
|
| 44 |
+
page_text = trafilatura.extract(requests.get(link).text)
|
| 45 |
+
if page_text and question.lower() in page_text.lower():
|
| 46 |
+
best_match = link
|
| 47 |
+
best_text = page_text
|
| 48 |
+
break # Stop at first good match
|
| 49 |
+
|
| 50 |
+
return best_match, best_text
|
| 51 |
+
|
| 52 |
+
# Step 5: Generate Answer using `smolagents`
|
| 53 |
+
def generate_answer(question, context):
|
| 54 |
+
agent = Agent("Question-Answering Agent", description="Answers questions based on documentation.")
|
| 55 |
+
prompt = f"""
|
| 56 |
+
Context: {context}
|
| 57 |
+
Question: {question}
|
| 58 |
+
Provide a clear and concise answer.
|
| 59 |
+
"""
|
| 60 |
+
return agent.run(prompt)
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
main()
|