Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Imports & Config
|
2 |
+
import streamlit as st
|
3 |
+
from openai import OpenAI
|
4 |
+
from llama_index.core import VectorStoreIndex, download_loader, SimpleDirectoryReader
|
5 |
+
from llama_index.readers.web import BeautifulSoupWebReader
|
6 |
+
from bs4 import BeautifulSoup
|
7 |
+
import requests
|
8 |
+
import openai
|
9 |
+
|
10 |
+
# Title
|
11 |
+
st.title("✈️ Airline Compensation Bot 💬")
|
12 |
+
|
13 |
+
# Introduction
|
14 |
+
st.markdown("**Flight delayed or cancelled yet again?**")
|
15 |
+
st.markdown("Don’t waste time digging through complicated airline policies! **Just tell us your airline, delay/cancellation reason, and any other relevant details and our chatbot will instantly tell you what compensation you're entitled to - by law and by airline policy.")
|
16 |
+
st.divider()
|
17 |
+
|
18 |
+
#OpenAI API Key
|
19 |
+
with st.sidebar:
|
20 |
+
openai_api_key = st.text_input("Enter your OpenAI API Key", type="password")
|
21 |
+
"[Get an OpenAI API key](https://platform.openai.com/account/api-keys)"
|
22 |
+
"[View the source code](https://github.com/streamlit/llm-examples/blob/main/Chatbot.py)"
|
23 |
+
|
24 |
+
# Check if an API key is provided
|
25 |
+
if not openai_api_key:
|
26 |
+
st.warning("Please enter your OpenAI API to use this app 🔑")
|
27 |
+
st.stop() # Stop the app until an API key is provided
|
28 |
+
|
29 |
+
# Initialize OpenAI API Client with user-provided API key
|
30 |
+
openai.api_key = openai_api_key
|
31 |
+
|
32 |
+
# Load data
|
33 |
+
loader = BeautifulSoupWebReader()
|
34 |
+
documents = loader.load_data(urls=[
|
35 |
+
"https://content.spirit.com/Shared/en-us/Documents/Contract_of_Carriage.pdf",
|
36 |
+
"https://secure.dot.gov/air-travel-complaint",
|
37 |
+
"https://www.ecfr.gov/current/title-14/chapter-II/subchapter-A/part-259",
|
38 |
+
"https://www.ecfr.gov/current/title-14/chapter-II/subchapter-A/part-259#259.5",
|
39 |
+
"https://www.federalregister.gov/documents/2024/04/26/2024-07177/refunds-and-other-consumer-protections",
|
40 |
+
"https://www.federalregister.gov/documents/2024/08/12/2024-17602/refunds-and-other-consumer-protections-2024-faa-reauthorization",
|
41 |
+
"https://www.flyfrontier.com/legal/customer-service-plan?mobile=true",
|
42 |
+
"https://www.hawaiianairlines.com/about-us/customer-service-plan",
|
43 |
+
"https://www.jetblue.com/customer-assurance/customer-service-plan",
|
44 |
+
"https://www.jetblue.com/legal/customer-service-plan",
|
45 |
+
"https://www.reginfo.gov/public/do/eAgendaViewRule?pubId=201910&RIN=2105-AE57",
|
46 |
+
"https://www.reginfo.gov/public/do/eAgendaViewRule?pubId=202110&RIN=2105-AF04",
|
47 |
+
"https://www.southwest.com/assets/pdfs/corporate-commitments/customer-service-plan.pdf?clk=7396032",
|
48 |
+
"https://www.southwest.com/swa-resources/pdfs/corporate-commitments/contract-of-carriage.pdf?clk=CSP_Form",
|
49 |
+
"https://www.transportation.gov/airconsumer",
|
50 |
+
"https://www.transportation.gov/airconsumer/air-travel-tips#FAQ",
|
51 |
+
"https://www.transportation.gov/airconsumer/air-travelers-tell-it-judge",
|
52 |
+
"https://www.transportation.gov/airconsumer/airline-consumer-contacts",
|
53 |
+
"https://www.transportation.gov/airconsumer/fly-rights",
|
54 |
+
"https://www.transportation.gov/individuals/aviation-consumer-protection/bumping-oversales",
|
55 |
+
"https://www.transportation.gov/individuals/aviation-consumer-protection/refunds",
|
56 |
+
"https://www.transportation.gov/individuals/aviation-consumer-protection/tarmac-delays",
|
57 |
+
"https://www.transportation.gov/lost-delayed-or-damaged-baggage",
|
58 |
+
"https://www.transportation.gov/resources/individuals/aviation-consumer-protection/airline-cancellation-delay-dashboard-html",
|
59 |
+
])
|
60 |
+
|
61 |
+
# RAG
|
62 |
+
index = VectorStoreIndex.from_documents(documents)
|
63 |
+
|
64 |
+
def response_generator(query):
|
65 |
+
try:
|
66 |
+
# Create an index from the documents
|
67 |
+
query_engine = index.as_query_engine()
|
68 |
+
response = query_engine.query(query)
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
# Log or handle the exception
|
72 |
+
response = f"An error occurred: {e}"
|
73 |
+
|
74 |
+
return response
|
75 |
+
|
76 |
+
|
77 |
+
# Initialize chat history
|
78 |
+
if "messages" not in st.session_state:
|
79 |
+
st.session_state.messages = []
|
80 |
+
|
81 |
+
# Display chat messages from history on app rerun
|
82 |
+
for message in st.session_state.messages:
|
83 |
+
with st.chat_message(message["role"]):
|
84 |
+
st.markdown(message["content"])
|
85 |
+
|
86 |
+
# Accept user input
|
87 |
+
if prompt := st.chat_input("How can I help you?"):
|
88 |
+
# Add user message to chat history
|
89 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
90 |
+
|
91 |
+
# Display user message in chat message container
|
92 |
+
with st.chat_message("user"):
|
93 |
+
st.markdown(prompt)
|
94 |
+
|
95 |
+
# Generate and display assistant response
|
96 |
+
# st.write('Before Response generator')
|
97 |
+
response = response_generator(prompt)
|
98 |
+
|
99 |
+
# Display assistant response in chat message container
|
100 |
+
with st.chat_message("assistant"):
|
101 |
+
st.markdown(response)
|
102 |
+
|
103 |
+
# Add assistant response to chat history
|
104 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|