Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Splitting the text page by page and then by line
|
23 |
+
lines = text.strip().split('\n')
|
24 |
+
|
25 |
+
# Find the start of the table, assuming it begins with a "Order ID" heading
|
26 |
+
start_index = next((i for i, line in enumerate(lines) if "Order ID" in line), None)
|
27 |
+
|
28 |
+
if start_index is not None:
|
29 |
+
# Table headers are on the same line
|
30 |
+
headers = [header.strip() for header in lines[start_index].split(",")]
|
31 |
+
|
32 |
+
# Data starts from the next line
|
33 |
+
for line in lines[start_index + 1:]:
|
34 |
+
values = [value.strip() for value in line.split(",")]
|
35 |
+
# Ensure that the number of values matches the number of headers
|
36 |
+
if len(headers) == len(values):
|
37 |
+
order_data.append(dict(zip(headers, values)))
|
38 |
+
return order_data
|
39 |
+
|
40 |
+
# Function to fetch order status using GROQ API
|
41 |
+
def fetch_order_status_from_groq(order_id, groq_api_key):
|
42 |
+
"""Fetches order status and customer details from GROQ API."""
|
43 |
+
|
44 |
+
transport = RequestsHTTPTransport(
|
45 |
+
url="https://api.groq.cloud/v1/graphql", # Replace with your GROQ endpoint
|
46 |
+
headers={"Authorization": f"Bearer {groq_api_key}"},
|
47 |
+
verify=True,
|
48 |
+
retries=3,
|
49 |
+
)
|
50 |
+
|
51 |
+
client = Client(transport=transport, fetch_schema_from_transport=True)
|
52 |
+
|
53 |
+
query = gql("""
|
54 |
+
query GetOrder($orderId: String!) {
|
55 |
+
getOrder(id: $orderId) {
|
56 |
+
id
|
57 |
+
status
|
58 |
+
customer {
|
59 |
+
name
|
60 |
+
email
|
61 |
+
}
|
62 |
+
}
|
63 |
+
}
|
64 |
+
""") # Replace with your GROQ query
|
65 |
+
|
66 |
+
variables = {"orderId": order_id}
|
67 |
+
|
68 |
+
try:
|
69 |
+
result = client.execute(query, variable_values=variables)
|
70 |
+
return result["getOrder"]
|
71 |
+
except Exception as e:
|
72 |
+
return f"Error fetching data from GROQ: {e}"
|
73 |
+
|
74 |
+
# Streamlit app
|
75 |
+
def main():
|
76 |
+
st.title("Order Status App")
|
77 |
+
|
78 |
+
uploaded_file = st.file_uploader("Upload Customer Orders PDF", type="pdf")
|
79 |
+
|
80 |
+
if uploaded_file is not None:
|
81 |
+
order_data = extract_order_data(uploaded_file)
|
82 |
+
if order_data:
|
83 |
+
st.success("Order data extracted successfully!")
|
84 |
+
df = pd.DataFrame(order_data)
|
85 |
+
st.dataframe(df) # Display the extracted data as a DataFrame
|
86 |
+
|
87 |
+
order_id_to_check = st.text_input("Enter Order ID to check status:")
|
88 |
+
if order_id_to_check:
|
89 |
+
order_status = fetch_order_status_from_groq(order_id_to_check, groq_api_key)
|
90 |
+
if order_status:
|
91 |
+
st.json(order_status)
|
92 |
+
else:
|
93 |
+
st.error("Could not retrieve order status.")
|
94 |
+
else:
|
95 |
+
st.error("Failed to extract order data from PDF.")
|
96 |
+
|
97 |
+
if __name__ == "__main__":
|
98 |
+
main()
|