File size: 2,018 Bytes
887ce17
 
 
 
 
 
 
721f77c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36b9982
721f77c
 
 
 
 
 
 
 
 
18e7160
721f77c
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from langchain_community.llms import HuggingFaceEndpoint

#When deployed on huggingface spaces, this values has to be passed using Variables & Secrets setting, as shown in the video :)
#import os
#os.environ["OPENAI_API_KEY"] = "sk-PLfFwPq6y24234234234FJ1Uc234234L8hVowXdt"

#from api import Api
#import streamlit as st 
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

#external class for api integrations, 
#api = Api()

#llm default OpenAPI
#llm = api.llm
llm = HuggingFaceEndpoint(repo_id="mistralai/Mistral-7B-Instruct-v0.2", Temperature=0.9)

#streamlit view components
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:
        
        #1 prompt template
        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})
            #json printed
            print(response)
            st.text(response)