Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# AIML API settings
|
| 6 |
+
aiml_api_key = os.getenv("AIML_API_KEY") # Fetch the API key from environment variables
|
| 7 |
+
client = OpenAI(api_key=aiml_api_key)
|
| 8 |
+
|
| 9 |
+
# Function to generate sentiment analysis
|
| 10 |
+
def generate_response(feedback, feedback_source):
|
| 11 |
+
prompt = f"Analyze the following {feedback_source} feedback and provide the sentiment (positive, negative, neutral) and key phrases: {feedback}"
|
| 12 |
+
chat_completion = client.chat.completions.create(
|
| 13 |
+
model="o1-mini",
|
| 14 |
+
messages=[
|
| 15 |
+
{"role": "user", "content": prompt},
|
| 16 |
+
],
|
| 17 |
+
max_tokens=1000,
|
| 18 |
+
)
|
| 19 |
+
return chat_completion.choices[0].message.content
|
| 20 |
+
|
| 21 |
+
# Streamlit app layout
|
| 22 |
+
st.title("Sentiment Analysis Tool")
|
| 23 |
+
|
| 24 |
+
# Category selection (for feedback context)
|
| 25 |
+
category = st.selectbox("Select your feedback source", ("Product Reviews", "Social Media", "Post-purchase Surveys"))
|
| 26 |
+
|
| 27 |
+
# Input for customer's feedback
|
| 28 |
+
query = st.text_area("Enter customer feedback for analysis", placeholder="Paste product review or social media comment here...")
|
| 29 |
+
|
| 30 |
+
# Button to trigger sentiment analysis
|
| 31 |
+
if st.button("Analyze Feedback"):
|
| 32 |
+
if query:
|
| 33 |
+
# Generate sentiment and key phrases
|
| 34 |
+
response = generate_response(query, category)
|
| 35 |
+
st.write(response)
|
| 36 |
+
else:
|
| 37 |
+
st.write("Please enter customer feedback.")
|