-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.py
79 lines (64 loc) · 2.39 KB
/
App.py
1
2
3
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import streamlit as st
import pyodbc
import pandas as pd
import matplotlib.pyplot as plt
import os
from dotenv import load_dotenv
import openai
# Load environment variables
load_dotenv()
# Database connection (replace with your credentials)
conn_str = (
r'DRIVER={ODBC Driver 17 for SQL Server};'
r'SERVER=' + os.getenv('SQL_SERVER') + ';'
r'DATABASE=' + os.getenv('SQL_DATABASE') + ';'
r'UID=' + os.getenv('SQL_USERNAME') + ';'
r'PWD=' + os.getenv('SQL_PASSWORD') + ';'
)
openai.api_key = os.getenv('OPENAI_API_KEY')
def analyze_sentiment(review):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Analyze the sentiment of this review: {review}. Return 'Positive', 'Negative', or 'Neutral'.",
max_tokens=30,
n=1,
stop=None,
temperature=0.5,
)
sentiment = response.choices[0].text.strip()
return sentiment
try:
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
st.title("Customer Review Sentiment Analysis")
# Fetch data from the database
cursor.execute("SELECT ReviewText, Sentiment FROM CustomerReviews")
rows = cursor.fetchall()
df = pd.DataFrame(rows, columns=["ReviewText", "Sentiment"])
# Display raw data (optional)
if st.checkbox("Show Raw Data"):
st.write(df)
# Sentiment distribution
sentiment_counts = df['Sentiment'].value_counts()
fig, ax = plt.subplots()
ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct='%1.1f%%', startangle=90)
ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
st.pyplot(fig)
#Example of adding a new review:
new_review = st.text_input("Enter a new review:")
if st.button("Analyze Review"):
if new_review:
new_sentiment = analyze_sentiment(new_review)
st.write(f"Sentiment: {new_sentiment}")
cursor.execute("INSERT INTO CustomerReviews (ReviewText,Sentiment) Values (?,?)", new_review, new_sentiment)
conn.commit()
st.success("Review added and analyzed!")
except pyodbc.Error as ex:
st.error(f"Database error: {ex}")
except openai.error.OpenAIError as e:
st.error(f"OpenAI API error: {e}")
except Exception as e:
st.exception(e) # Show detailed error information
finally:
if conn:
conn.close()