Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from langchain_groq import ChatGroq
|
| 6 |
+
from langchain import hub
|
| 7 |
+
from langchain_chroma import Chroma
|
| 8 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 9 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 10 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 11 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 12 |
+
from sentence_transformers import SentenceTransformer
|
| 13 |
+
import bs4
|
| 14 |
+
import torch
|
| 15 |
+
|
| 16 |
+
# Initialize the Groq client
|
| 17 |
+
client = Groq(api_key="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")
|
| 18 |
+
|
| 19 |
+
def generate_response_groq(context, query):
|
| 20 |
+
"""Generate response using Groq API."""
|
| 21 |
+
prompt = f"Context: {context}\nQuestion: {query}\nAnswer:"
|
| 22 |
+
chat_completion = client.chat.completions.create(
|
| 23 |
+
messages=[
|
| 24 |
+
{
|
| 25 |
+
"role": "user",
|
| 26 |
+
"content": prompt,
|
| 27 |
+
}
|
| 28 |
+
],
|
| 29 |
+
model="llama3-8b-8192",
|
| 30 |
+
)
|
| 31 |
+
response = chat_completion.choices[0].message.content
|
| 32 |
+
return response
|
| 33 |
+
|
| 34 |
+
def extract_text_from_url(url):
|
| 35 |
+
"""Extract text from URL using WebBaseLoader."""
|
| 36 |
+
loader = WebBaseLoader(
|
| 37 |
+
web_paths=(url,),
|
| 38 |
+
bs_kwargs=dict(
|
| 39 |
+
parse_only=bs4.SoupStrainer()
|
| 40 |
+
),
|
| 41 |
+
)
|
| 42 |
+
docs = loader.load()
|
| 43 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 44 |
+
splits = text_splitter.split_documents(docs)
|
| 45 |
+
text = ""
|
| 46 |
+
for split in splits:
|
| 47 |
+
text += split.page_content
|
| 48 |
+
return text
|
| 49 |
+
|
| 50 |
+
# Set the page layout to wide for better UI space
|
| 51 |
+
st.set_page_config(page_title="URL Query Application", layout="wide")
|
| 52 |
+
|
| 53 |
+
# # Sidebar
|
| 54 |
+
# st.sidebar.title("URL Query Assistant")
|
| 55 |
+
# st.sidebar.image("https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Globe_icon_2.svg/2048px-Globe_icon_2.svg.png", use_column_width=True) # Adding an image in the sidebar
|
| 56 |
+
# st.sidebar.markdown("### Navigation")
|
| 57 |
+
# st.sidebar.markdown("Use this app to ask questions about the content of a predefined URL.")
|
| 58 |
+
# st.sidebar.markdown("")
|
| 59 |
+
|
| 60 |
+
# Main UI layout
|
| 61 |
+
st.title("🌐 URL Query Application")
|
| 62 |
+
st.markdown("""
|
| 63 |
+
<style>
|
| 64 |
+
.main-content {background-color: #f0f2f6; padding: 20px; border-radius: 10px;}
|
| 65 |
+
.stButton>button {background-color: #4CAF50; color: white; font-size: 16px; border-radius: 10px;}
|
| 66 |
+
.stTextInput>div>div>input {background-color: #f0f2f6; color: black; border-radius: 5px;}
|
| 67 |
+
</style>
|
| 68 |
+
""", unsafe_allow_html=True)
|
| 69 |
+
|
| 70 |
+
st.markdown("<div class='main-content'>", unsafe_allow_html=True)
|
| 71 |
+
|
| 72 |
+
url = "https://iesco.com.pk/index.php/customer-services/tariff-guide#"
|
| 73 |
+
document_text = extract_text_from_url(url)
|
| 74 |
+
st.text_area("📜 Extracted Text", document_text, height=200)
|
| 75 |
+
|
| 76 |
+
query = st.text_input("🔍 Enter your query")
|
| 77 |
+
|
| 78 |
+
if st.button("💬 Get Answer"):
|
| 79 |
+
if query:
|
| 80 |
+
with st.spinner("Generating response..."):
|
| 81 |
+
response = generate_response_groq(document_text, query)
|
| 82 |
+
st.write("**Response:**")
|
| 83 |
+
st.write(response)
|
| 84 |
+
else:
|
| 85 |
+
st.error("Please enter a query.")
|
| 86 |
+
|
| 87 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 88 |
+
|
| 89 |
+
# # Footer
|
| 90 |
+
# st.sidebar.markdown("### About")
|
| 91 |
+
# st.sidebar.info("Developed using Streamlit and Groq API.")
|
| 92 |
+
# st.sidebar.markdown("---")
|
| 93 |
+
# st.sidebar.write("For more information, visit [Groq](https://www.groq.com) and [Streamlit](https://streamlit.io).")
|
| 94 |
+
|
| 95 |
+
# Customize the theme and color contrast
|
| 96 |
+
st.markdown("""
|
| 97 |
+
<style>
|
| 98 |
+
.css-1aumxhk {background-color: #E8EAF6;}
|
| 99 |
+
.stTextInput>div>div>input {border-color: #3f51b5;}
|
| 100 |
+
.stTextArea>div>div>textarea {border-color: #3f51b5;}
|
| 101 |
+
.stButton>button {background-color: #3f51b5; color: white; font-size: 16px;}
|
| 102 |
+
</style>
|
| 103 |
+
""", unsafe_allow_html=True)
|