Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
import faiss
|
| 7 |
+
from groq import Groq
|
| 8 |
+
|
| 9 |
+
# Scrape data from the webpage
|
| 10 |
+
def scrape_tariff_data(url):
|
| 11 |
+
response = requests.get(url)
|
| 12 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 13 |
+
tariff_data = []
|
| 14 |
+
for paragraph in soup.find_all('p'):
|
| 15 |
+
tariff_data.append(paragraph.text.strip())
|
| 16 |
+
return "\n".join(tariff_data)
|
| 17 |
+
|
| 18 |
+
# Chunking text
|
| 19 |
+
def chunk_text(text, max_length=512):
|
| 20 |
+
words = text.split()
|
| 21 |
+
chunks = []
|
| 22 |
+
for i in range(0, len(words), max_length):
|
| 23 |
+
chunks.append(" ".join(words[i:i+max_length]))
|
| 24 |
+
return chunks
|
| 25 |
+
|
| 26 |
+
# Embed text and save in FAISS
|
| 27 |
+
def create_faiss_index(chunks, model_name='all-MiniLM-L6-v2'):
|
| 28 |
+
model = SentenceTransformer(model_name)
|
| 29 |
+
embeddings = model.encode(chunks)
|
| 30 |
+
dimension = embeddings.shape[1]
|
| 31 |
+
index = faiss.IndexFlatL2(dimension)
|
| 32 |
+
index.add(embeddings)
|
| 33 |
+
return index, embeddings, model
|
| 34 |
+
|
| 35 |
+
# Groq API for LLM
|
| 36 |
+
def query_llm(prompt, api_key):
|
| 37 |
+
client = Groq(api_key=api_key)
|
| 38 |
+
chat_completion = client.chat.completions.create(
|
| 39 |
+
messages=[
|
| 40 |
+
{
|
| 41 |
+
"role": "user",
|
| 42 |
+
"content": prompt,
|
| 43 |
+
}
|
| 44 |
+
],
|
| 45 |
+
model="llama3-8b-8192",
|
| 46 |
+
)
|
| 47 |
+
return chat_completion.choices[0].message.content
|
| 48 |
+
|
| 49 |
+
# Streamlit UI
|
| 50 |
+
st.title("RAG-Based Tariff Data Application")
|
| 51 |
+
|
| 52 |
+
url = st.text_input("Enter Tariff Data URL", "https://iesco.com.pk/index.php/customer-services/tariff-guide")
|
| 53 |
+
api_key = st.text_input("Enter Groq API Key", type="password")
|
| 54 |
+
|
| 55 |
+
if st.button("Process Tariff Data"):
|
| 56 |
+
with st.spinner("Extracting and processing data..."):
|
| 57 |
+
text = scrape_tariff_data(url)
|
| 58 |
+
chunks = chunk_text(text)
|
| 59 |
+
index, embeddings, model = create_faiss_index(chunks)
|
| 60 |
+
st.success("Data processed and indexed!")
|
| 61 |
+
|
| 62 |
+
if st.text_input("Query"):
|
| 63 |
+
prompt = st.text_input("Enter your query")
|
| 64 |
+
if prompt and api_key:
|
| 65 |
+
with st.spinner("Fetching response..."):
|
| 66 |
+
response = query_llm(prompt, api_key)
|
| 67 |
+
st.write(response)
|