Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,100 +1,76 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import PyPDF2
|
3 |
import os
|
4 |
-
|
5 |
-
from
|
6 |
-
|
7 |
import pandas as pd
|
|
|
|
|
|
|
8 |
|
9 |
-
# Load environment variables (GROQ API Key)
|
10 |
load_dotenv()
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
def main():
|
78 |
-
st.title("Order Status App")
|
79 |
-
|
80 |
-
uploaded_file = st.file_uploader("Upload Customer Orders PDF", type="pdf")
|
81 |
-
|
82 |
-
if uploaded_file is not None:
|
83 |
-
order_data = extract_order_data(uploaded_file)
|
84 |
-
if order_data:
|
85 |
-
st.success("Order data extracted successfully!")
|
86 |
-
df = pd.DataFrame(order_data)
|
87 |
-
st.dataframe(df) # Display the extracted data as a DataFrame
|
88 |
-
|
89 |
-
order_id_to_check = st.text_input("Enter Order ID to check status:")
|
90 |
-
if order_id_to_check:
|
91 |
-
order_status = fetch_order_status_from_groq(order_id_to_check, groq_api_key)
|
92 |
-
if order_status:
|
93 |
-
st.json(order_status)
|
94 |
-
else:
|
95 |
-
st.error("Could not retrieve order status.")
|
96 |
-
else:
|
97 |
-
st.error("Failed to extract order data from PDF. Please check the PDF format and try again.")
|
98 |
-
|
99 |
-
if __name__ == "__main__":
|
100 |
-
main()
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import streamlit as st
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
import docx
|
5 |
import pandas as pd
|
6 |
+
from bs4 import BeautifulSoup
|
7 |
+
import openai
|
8 |
+
from dotenv import load_dotenv
|
9 |
|
|
|
10 |
load_dotenv()
|
11 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
12 |
+
openai.api_key = GROQ_API_KEY
|
13 |
+
openai.api_base = "https://api.groq.com/openai/v1"
|
14 |
+
|
15 |
+
st.set_page_config(page_title="ToyShop Order Assistant", layout="wide")
|
16 |
+
st.title("🧸 Online Toy Shop - Order Status Assistant")
|
17 |
+
|
18 |
+
st.sidebar.header("Upload Customer Order Files")
|
19 |
+
uploaded_files = st.sidebar.file_uploader(
|
20 |
+
"Upload your customer order files",
|
21 |
+
type=["pdf", "docx", "txt", "xlsx", "html"],
|
22 |
+
accept_multiple_files=True
|
23 |
+
)
|
24 |
+
|
25 |
+
def extract_text(file):
|
26 |
+
if file.name.endswith(".pdf"):
|
27 |
+
reader = PdfReader(file)
|
28 |
+
return "\n".join(page.extract_text() or "" for page in reader.pages)
|
29 |
+
elif file.name.endswith(".docx"):
|
30 |
+
doc = docx.Document(file)
|
31 |
+
return "\n".join(p.text for p in doc.paragraphs)
|
32 |
+
elif file.name.endswith(".txt"):
|
33 |
+
return file.read().decode("utf-8")
|
34 |
+
elif file.name.endswith(".xlsx"):
|
35 |
+
df = pd.read_excel(file)
|
36 |
+
return df.to_string()
|
37 |
+
elif file.name.endswith(".html"):
|
38 |
+
soup = BeautifulSoup(file.read(), "html.parser")
|
39 |
+
return soup.get_text()
|
40 |
+
else:
|
41 |
+
return ""
|
42 |
+
|
43 |
+
combined_text = ""
|
44 |
+
if uploaded_files:
|
45 |
+
st.sidebar.success(f"{len(uploaded_files)} file(s) uploaded.")
|
46 |
+
for f in uploaded_files:
|
47 |
+
try:
|
48 |
+
combined_text += f"\n\n--- {f.name} ---\n\n"
|
49 |
+
combined_text += extract_text(f)
|
50 |
+
except Exception as e:
|
51 |
+
st.sidebar.error(f"Error reading {f.name}: {str(e)}")
|
52 |
+
|
53 |
+
query = st.text_input("Ask about your order (e.g., 'What is the status of order #123?')")
|
54 |
+
|
55 |
+
if query and combined_text:
|
56 |
+
with st.spinner("Thinking..."):
|
57 |
+
try:
|
58 |
+
system_prompt = (
|
59 |
+
"You are a helpful assistant for an online toy shop. "
|
60 |
+
"Answer customer queries based on the following order information:\n\n"
|
61 |
+
+ combined_text
|
62 |
+
)
|
63 |
+
response = openai.ChatCompletion.create(
|
64 |
+
model="llama3-8b-8192",
|
65 |
+
messages=[
|
66 |
+
{"role": "system", "content": system_prompt},
|
67 |
+
{"role": "user", "content": query}
|
68 |
+
]
|
69 |
+
)
|
70 |
+
answer = response['choices'][0]['message']['content']
|
71 |
+
st.success("Answer:")
|
72 |
+
st.write(answer)
|
73 |
+
except Exception as e:
|
74 |
+
st.error(f"Error: {str(e)}")
|
75 |
+
elif query:
|
76 |
+
st.warning("Please upload order files to enable RAG-based answers.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|