Spaces:
Sleeping
Sleeping
Update customquery.py
Browse files- customquery.py +56 -55
customquery.py
CHANGED
|
@@ -1,55 +1,56 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import openai
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 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 |
-
st.write(
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import openai
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Set up OpenAI API
|
| 7 |
+
openai.api_key = os.environ["key"]
|
| 8 |
+
|
| 9 |
+
# Function to query ChatGPT with a specific question and dataset context
|
| 10 |
+
def query_chatgpt(question, context):
|
| 11 |
+
prompt = f"""
|
| 12 |
+
Given the following dataset:
|
| 13 |
+
{context}
|
| 14 |
+
|
| 15 |
+
Answer the following question consisely and write your final calculation:
|
| 16 |
+
{question}
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
prompt2="""You are a teacher who excels in statistics
|
| 20 |
+
after recieving the data you have to do calculations and answer the query
|
| 21 |
+
asked by the user you are the best in analyzing data in whole world
|
| 22 |
+
You do not have to show how you are calculating the answers"""
|
| 23 |
+
|
| 24 |
+
response = openai.chat.completions.create(
|
| 25 |
+
model="gpt-3.5-turbo",
|
| 26 |
+
messages=[
|
| 27 |
+
{'role':"system","content":prompt2},
|
| 28 |
+
{"role": "user", "content": prompt}
|
| 29 |
+
]
|
| 30 |
+
)
|
| 31 |
+
return response.choices[0].message.content
|
| 32 |
+
|
| 33 |
+
# Streamlit app
|
| 34 |
+
|
| 35 |
+
upload_file = st.file_uploader("Upload CSV file with student data", type="csv")
|
| 36 |
+
|
| 37 |
+
if upload_file is not None:
|
| 38 |
+
# Load the data
|
| 39 |
+
df = pd.read_csv(uploaded_file)
|
| 40 |
+
|
| 41 |
+
# Display the dataframe
|
| 42 |
+
st.write("### Uploaded Data", df)
|
| 43 |
+
|
| 44 |
+
# Ask the teacher to input a question
|
| 45 |
+
question = st.text_area("Ask a question about the dataset:")
|
| 46 |
+
|
| 47 |
+
if st.button("Get Answer"):
|
| 48 |
+
# Convert dataframe to a string format
|
| 49 |
+
context = df.to_string(index=False)
|
| 50 |
+
|
| 51 |
+
# Query ChatGPT
|
| 52 |
+
answer = query_chatgpt(question, context)
|
| 53 |
+
|
| 54 |
+
# Display the answer
|
| 55 |
+
st.write("### Answer from ChatGPT")
|
| 56 |
+
st.write(answer)
|