Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import os
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from streamlit_chat import message as st_message # Ensure streamlit_chat is installed
|
| 6 |
+
|
| 7 |
+
# Load data function remains the same
|
| 8 |
+
def load_data(path):
|
| 9 |
+
return pd.read_csv(path)
|
| 10 |
+
|
| 11 |
+
# Assuming you've set your OpenAI API key in the environment variables
|
| 12 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# File uploader and data loading logic can remain the same
|
| 16 |
+
uploaded_file = st.sidebar.file_uploader("Choose a CSV file", type="csv")
|
| 17 |
+
if uploaded_file is not None:
|
| 18 |
+
st.session_state["df"] = pd.read_csv(uploaded_file)
|
| 19 |
+
|
| 20 |
+
# Example function to generate a response from OpenAI's chat model
|
| 21 |
+
def ask_openai(prompt):
|
| 22 |
+
try:
|
| 23 |
+
response = openai.ChatCompletion.create(
|
| 24 |
+
model="gpt-3.5-turbo", # Adjust model as needed
|
| 25 |
+
messages=[{"role": "system", "content": "You are a helpful assistant."},
|
| 26 |
+
{"role": "user", "content": prompt}],
|
| 27 |
+
)
|
| 28 |
+
return response.choices[0].message["content"]
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error in generating response: {e}")
|
| 31 |
+
return "Sorry, I couldn't generate a response. Please try again."
|
| 32 |
+
|
| 33 |
+
# Example chat interaction in Streamlit
|
| 34 |
+
if "chat_history" not in st.session_state:
|
| 35 |
+
st.session_state["chat_history"] = []
|
| 36 |
+
|
| 37 |
+
if prompt := st.text_input("Ask me anything about the data:"):
|
| 38 |
+
st.session_state["chat_history"].append({"role": "user", "content": prompt})
|
| 39 |
+
response = ask_openai(prompt)
|
| 40 |
+
st.session_state["chat_history"].append({"role": "assistant", "content": response})
|
| 41 |
+
|
| 42 |
+
for chat in st.session_state["chat_history"]:
|
| 43 |
+
st_message(chat["content"], is_user=True if chat["role"] == "user" else False)
|