masadonline commited on
Commit
36c0c0f
·
verified ·
1 Parent(s): 6c38165

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -96
app.py CHANGED
@@ -1,100 +1,76 @@
1
- import streamlit as st
2
- import PyPDF2
3
  import os
4
- from dotenv import load_dotenv
5
- from gql import gql, Client
6
- from gql.transport.requests import RequestsHTTPTransport
7
  import pandas as pd
 
 
 
8
 
9
- # Load environment variables (GROQ API Key)
10
  load_dotenv()
11
- groq_api_key = os.environ.get("GROQ_API_KEY")
12
-
13
- # Function to extract order data from PDF
14
- def extract_order_data(pdf_file):
15
- """Extracts order data from the uploaded PDF file."""
16
-
17
- order_data = []
18
- reader = PyPDF2.PdfReader(pdf_file)
19
- for page in reader.pages:
20
- text = page.extract_text()
21
- if text:
22
- lines = text.strip().split('\n')
23
- start_index = next((i for i, line in enumerate(lines) if "Order ID" in line), None)
24
-
25
- if start_index is not None:
26
- headers = [header.strip() for header in lines[start_index].split(",")]
27
- # Clean headers from extra spaces
28
- headers = [h.replace(" ", "") for h in headers] # Remove spaces in header names
29
- for line in lines[start_index + 1:]:
30
- values = [v.strip() for v in line.split(",")]
31
- if len(headers) == len(values):
32
- order_data.append(dict(zip(headers, values)))
33
- elif len(values) > len(headers):
34
- # Handle cases where there are more values than headers (e.g., extra commas)
35
- order_data.append(dict(zip(headers, values[:len(headers)])))
36
- else:
37
- print(f"Skipping line due to header/value mismatch: {line}") # print the problematic line.
38
- return order_data
39
-
40
-
41
-
42
- # Function to fetch order status using GROQ API
43
- def fetch_order_status_from_groq(order_id, groq_api_key):
44
- """Fetches order status and customer details from GROQ API."""
45
-
46
- transport = RequestsHTTPTransport(
47
- url="[https://api.groq.cloud/v1/graphql](https://api.groq.cloud/v1/graphql)", # Replace with your GROQ endpoint
48
- headers={"Authorization": f"Bearer {groq_api_key}"},
49
- verify=True,
50
- retries=3,
51
- )
52
-
53
- client = Client(transport=transport, fetch_schema_from_transport=True)
54
-
55
- query = gql("""
56
- query GetOrder($orderId: String!) {
57
- getOrder(id: $orderId) {
58
- id
59
- status
60
- customer {
61
- name
62
- email
63
- }
64
- }
65
- }
66
- """) # Replace with your GROQ query
67
-
68
- variables = {"orderId": order_id}
69
-
70
- try:
71
- result = client.execute(query, variable_values=variables)
72
- return result["getOrder"]
73
- except Exception as e:
74
- return f"Error fetching data from GROQ: {e}"
75
-
76
- # Streamlit app
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.")