Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from sentence_transformers import SentenceTransformer
|
|
| 6 |
import faiss
|
| 7 |
from groq import Groq
|
| 8 |
|
| 9 |
-
#
|
| 10 |
def scrape_tariff_data(url):
|
| 11 |
response = requests.get(url)
|
| 12 |
soup = BeautifulSoup(response.text, 'html.parser')
|
|
@@ -15,7 +15,7 @@ def scrape_tariff_data(url):
|
|
| 15 |
tariff_data.append(paragraph.text.strip())
|
| 16 |
return "\n".join(tariff_data)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
def chunk_text(text, max_length=512):
|
| 20 |
words = text.split()
|
| 21 |
chunks = []
|
|
@@ -23,7 +23,7 @@ def chunk_text(text, max_length=512):
|
|
| 23 |
chunks.append(" ".join(words[i:i+max_length]))
|
| 24 |
return chunks
|
| 25 |
|
| 26 |
-
#
|
| 27 |
def create_faiss_index(chunks, model_name='all-MiniLM-L6-v2'):
|
| 28 |
model = SentenceTransformer(model_name)
|
| 29 |
embeddings = model.encode(chunks)
|
|
@@ -32,8 +32,14 @@ def create_faiss_index(chunks, model_name='all-MiniLM-L6-v2'):
|
|
| 32 |
index.add(embeddings)
|
| 33 |
return index, embeddings, model
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
def query_llm(prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
client = Groq(api_key=api_key)
|
| 38 |
chat_completion = client.chat.completions.create(
|
| 39 |
messages=[
|
|
@@ -50,21 +56,27 @@ def query_llm(prompt, api_key):
|
|
| 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 |
-
GROQ_API_KEY= os.environ.get('GroqApi')
|
| 55 |
-
client = Groq(api_key=GROQ_API_KEY)
|
| 56 |
-
|
| 57 |
|
| 58 |
if st.button("Process Tariff Data"):
|
| 59 |
with st.spinner("Extracting and processing data..."):
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
if st.
|
| 66 |
-
|
| 67 |
-
if prompt and api_key:
|
| 68 |
with st.spinner("Fetching response..."):
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import faiss
|
| 7 |
from groq import Groq
|
| 8 |
|
| 9 |
+
# Function to scrape tariff data
|
| 10 |
def scrape_tariff_data(url):
|
| 11 |
response = requests.get(url)
|
| 12 |
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
|
| 15 |
tariff_data.append(paragraph.text.strip())
|
| 16 |
return "\n".join(tariff_data)
|
| 17 |
|
| 18 |
+
# Function to chunk text into manageable sizes
|
| 19 |
def chunk_text(text, max_length=512):
|
| 20 |
words = text.split()
|
| 21 |
chunks = []
|
|
|
|
| 23 |
chunks.append(" ".join(words[i:i+max_length]))
|
| 24 |
return chunks
|
| 25 |
|
| 26 |
+
# Function to create embeddings and FAISS index
|
| 27 |
def create_faiss_index(chunks, model_name='all-MiniLM-L6-v2'):
|
| 28 |
model = SentenceTransformer(model_name)
|
| 29 |
embeddings = model.encode(chunks)
|
|
|
|
| 32 |
index.add(embeddings)
|
| 33 |
return index, embeddings, model
|
| 34 |
|
| 35 |
+
# Function to query the Groq API
|
| 36 |
+
def query_llm(prompt):
|
| 37 |
+
GROQ_API_KEY= os.environ.get('GroqApi')
|
| 38 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 39 |
+
|
| 40 |
+
if not api_key:
|
| 41 |
+
return "Error: GROQ_API_KEY is not set in environment variables."
|
| 42 |
+
|
| 43 |
client = Groq(api_key=api_key)
|
| 44 |
chat_completion = client.chat.completions.create(
|
| 45 |
messages=[
|
|
|
|
| 56 |
st.title("RAG-Based Tariff Data Application")
|
| 57 |
|
| 58 |
url = st.text_input("Enter Tariff Data URL", "https://iesco.com.pk/index.php/customer-services/tariff-guide")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
if st.button("Process Tariff Data"):
|
| 61 |
with st.spinner("Extracting and processing data..."):
|
| 62 |
+
try:
|
| 63 |
+
text = scrape_tariff_data(url)
|
| 64 |
+
chunks = chunk_text(text)
|
| 65 |
+
index, embeddings, model = create_faiss_index(chunks)
|
| 66 |
+
st.success("Data processed and indexed!")
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.error(f"Error processing data: {e}")
|
| 69 |
+
|
| 70 |
+
st.header("Query the Tariff Data")
|
| 71 |
+
prompt = st.text_input("Enter your query")
|
| 72 |
|
| 73 |
+
if st.button("Get Answer"):
|
| 74 |
+
if prompt:
|
|
|
|
| 75 |
with st.spinner("Fetching response..."):
|
| 76 |
+
try:
|
| 77 |
+
response = query_llm(prompt)
|
| 78 |
+
st.write(response)
|
| 79 |
+
except Exception as e:
|
| 80 |
+
st.error(f"Error querying the model: {e}")
|
| 81 |
+
else:
|
| 82 |
+
st.warning("Please enter a query to continue.")
|