Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,59 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
#from langchain_openai import OpenAI
|
| 4 |
-
#from langchain.llms import HuggingFaceEndpoint
|
| 5 |
from langchain_community.llms import HuggingFaceEndpoint
|
| 6 |
|
| 7 |
#When deployed on huggingface spaces, this values has to be passed using Variables & Secrets setting, as shown in the video :)
|
| 8 |
#import os
|
| 9 |
#os.environ["OPENAI_API_KEY"] = "sk-PLfFwPq6y24234234234FJ1Uc234234L8hVowXdt"
|
| 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 |
-
if
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
from langchain_community.llms import HuggingFaceEndpoint
|
| 3 |
|
| 4 |
#When deployed on huggingface spaces, this values has to be passed using Variables & Secrets setting, as shown in the video :)
|
| 5 |
#import os
|
| 6 |
#os.environ["OPENAI_API_KEY"] = "sk-PLfFwPq6y24234234234FJ1Uc234234L8hVowXdt"
|
| 7 |
|
| 8 |
+
#from api import Api
|
| 9 |
+
#import streamlit as st
|
| 10 |
+
from langchain.prompts import PromptTemplate
|
| 11 |
+
from langchain.chains import LLMChain
|
| 12 |
+
|
| 13 |
+
#external class for api integrations,
|
| 14 |
+
#api = Api()
|
| 15 |
+
|
| 16 |
+
#llm default OpenAPI
|
| 17 |
+
#llm = api.llm
|
| 18 |
+
llm = HuggingFaceEndpoint(repo_id="mistralai/Mistral-7B-Instruct-v0.2", Temperature=0.9)
|
| 19 |
+
|
| 20 |
+
#streamlit view components
|
| 21 |
+
with st.form("my_form"):
|
| 22 |
+
st.title('Sentiment Analysis')
|
| 23 |
+
text_review = st.text_area('Write me a review')
|
| 24 |
+
|
| 25 |
+
option = st.selectbox(
|
| 26 |
+
'Select the language to evaluate:',
|
| 27 |
+
('Italian', 'Spanish', 'English'))
|
| 28 |
+
submitted = st.form_submit_button("Submit")
|
| 29 |
+
if submitted:
|
| 30 |
+
|
| 31 |
+
#1 prompt template
|
| 32 |
+
template = """
|
| 33 |
+
Please act as a machine learning model trained for perform a supervised learning task,
|
| 34 |
+
for extract the sentiment of a review in '{option}' Language.
|
| 35 |
+
|
| 36 |
+
Give your answer writing a Json evaluating the sentiment field between the dollar sign, the value must be printed without dollar sign.
|
| 37 |
+
The value of sentiment must be "positive" or "negative", otherwise if the text is not valuable write "null".
|
| 38 |
+
|
| 39 |
+
Example:
|
| 40 |
+
|
| 41 |
+
field 1 named :
|
| 42 |
+
text_review with value: {text_review}
|
| 43 |
+
field 2 named :
|
| 44 |
+
sentiment with value: $sentiment$
|
| 45 |
+
Field 3 named :
|
| 46 |
+
language with value: {option}
|
| 47 |
+
Review text: '''{text_review}'''
|
| 48 |
+
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
prompt = PromptTemplate(template=template, input_variables=["text_review","option"])
|
| 52 |
+
|
| 53 |
+
llm_chain = LLMChain(prompt=prompt, llm=llm)
|
| 54 |
+
|
| 55 |
+
if prompt:
|
| 56 |
+
response = llm_chain.run({"text_review": text_review, "option": option})
|
| 57 |
+
#json printed
|
| 58 |
+
print(response)
|
| 59 |
+
st.text(response)
|