File size: 2,065 Bytes
d1163f7 |
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 |
# app.py
import streamlit as st
from langchain.llms import Ollama
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from textblob import TextBlob
from dotenv import load_dotenv
load_dotenv()
def load_model():
model = Ollama(model='llama3.1:latest', temperature=0.2)
return model
def summarize_prompt():
return PromptTemplate(
input_variables=["email"],
template=(
"""
1. Berdasarkan teks berikut, buat ringkasan singkat tentang tindakan utama yang dilakukan oleh user, ambil no resi pada text dalam format berikut:
[Aksi User]
2. Berikan juga analisis sentimen percakapan user dengan salah satu label:
- Positif
- Negatif
- Netral
Teks:
{email}
Ringkasan:
Aksi User: [Deskripsikan tindakan utama user berdasarkan teks]
Sentimen: [Tentukan sentimen berdasarkan nada dan konteks teks]
"""
)
)
def simple_sentiment_analysis(text):
blob = TextBlob(text)
sentiment = blob.sentiment.polarity
if sentiment > 0:
return "Positif"
elif sentiment < 0:
return "Negatif"
else:
return "Netral"
model = load_model()
prompt = summarize_prompt()
llm_chain = LLMChain(llm=model, prompt=prompt)
def analyze_email(email):
result = llm_chain.run(email=email)
sentiment = simple_sentiment_analysis(email)
return result + f"\nSentimen Percakapan: {sentiment}"
# Streamlit UI
st.title("Sentiment Analysis and User Action Summarizer")
st.write("Enter an email or text below to analyze the user's action and sentiment:")
user_input = st.text_area("Email/Text Input", "", height=200)
if st.button("Analyze"):
if user_input:
analysis_result = analyze_email(user_input)
st.subheader("Analysis Result:")
st.text(analysis_result)
else:
st.warning("Please enter some text to analyze.")
|