Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import asyncio
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# Assume these functions exist in your scraper module
|
6 |
+
from main import process_urls, store_embeddings, embed_text, query_llm
|
7 |
+
|
8 |
+
# Streamlit UI
|
9 |
+
st.title("Web Scraper & AI Query Interface")
|
10 |
+
|
11 |
+
urls = st.text_area("Enter URLs (one per line)", "https://en.wikipedia.org/wiki/Nigeria\nhttps://en.wikipedia.org/wiki/Ghana")
|
12 |
+
query = st.text_input("Enter your question", "Where is Nigeria located?")
|
13 |
+
|
14 |
+
if st.button("Run Scraper"):
|
15 |
+
st.write("Fetching and processing URLs...")
|
16 |
+
|
17 |
+
async def run_scraper():
|
18 |
+
url_list = urls.split("\n")
|
19 |
+
split_docs = await process_urls(url_list)
|
20 |
+
index, text_data, text_sources = store_embeddings(split_docs)
|
21 |
+
return index, text_data, text_sources
|
22 |
+
|
23 |
+
# Run async function inside Streamlit
|
24 |
+
index, text_data, text_sources = asyncio.run(run_scraper())
|
25 |
+
|
26 |
+
st.write("Data processed! Now you can ask questions about the scraped content.")
|
27 |
+
user_query = st.text_input("Ask a question about the scraped data")
|
28 |
+
|
29 |
+
if st.button("Query Model"):
|
30 |
+
query_embedding = np.array([embed_text([user_query])[0]]).reshape(1, -1)
|
31 |
+
result = query_llm(index, text_data, text_sources, user_query)
|
32 |
+
|
33 |
+
for entry in result:
|
34 |
+
st.subheader(f"Source: {entry['source']}")
|
35 |
+
st.write(f"Response: {entry['response'].content}")
|