|
import streamlit as st |
|
from langchain_community.llms import HuggingFaceEndpoint |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from langchain.prompts import PromptTemplate |
|
from langchain.chains import LLMChain |
|
|
|
|
|
|
|
|
|
|
|
|
|
llm = HuggingFaceEndpoint(repo_id="mistralai/Mistral-7B-Instruct-v0.2", Temperature=0.9) |
|
|
|
|
|
with st.form("my_form"): |
|
st.title('Sentiment Analysis') |
|
text_review = st.text_area('Write me a review') |
|
|
|
option = st.selectbox( |
|
'Select the language to evaluate:', |
|
('Italian', 'Spanish', 'English')) |
|
submitted = st.form_submit_button("Submit") |
|
if submitted: |
|
|
|
|
|
template = """ |
|
Please act as a machine learning model trained for perform a supervised learning task, |
|
for extract the sentiment of a review in '{option}' Language. |
|
|
|
Give your answer writing a Json evaluating the sentiment field between the dollar sign, the value must be printed without dollar sign. |
|
The value of sentiment must be "Positive" or "Negative" otherwise if the text is not valuable write "Neutral". |
|
|
|
Example: |
|
|
|
field 1 named : |
|
text_review with value: {text_review} |
|
field 2 named : |
|
sentiment with value: $sentiment$ |
|
Field 3 named : |
|
language with value: {option} |
|
Review text: '''{text_review}''' |
|
|
|
""" |
|
|
|
prompt = PromptTemplate(template=template, input_variables=["text_review","option"]) |
|
|
|
llm_chain = LLMChain(prompt=prompt, llm=llm) |
|
|
|
if prompt: |
|
response = llm_chain.run({"text_review": text_review, "option": option}) |
|
|
|
print(response) |
|
st.text(response) |
|
|