samim2024 commited on
Commit
721f77c
·
verified ·
1 Parent(s): 4ee4b88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -37
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
- #Function to return the response
12
- def load_answer(question):
13
- # "text-davinci-003" model is depreciated, so using the latest one https://platform.openai.com/docs/deprecations
14
- #llm = OpenAI(model_name="gpt-3.5-turbo-instruct",temperature=0)
15
- llm = HuggingFaceEndpoint(repo_id="mistralai/Mistral-7B-Instruct-v0.2", Temperature=0.9)
16
- #Last week langchain has recommended to use invoke function for the below please :)
17
- answer=llm.invoke(question)
18
- return answer
19
-
20
-
21
- #App UI starts here
22
- st.set_page_config(page_title="Sentiment Analysis", page_icon=":robot:")
23
- st.header("Sentiment Analysis")
24
-
25
- #Gets the user input
26
- def get_text():
27
- input_text = st.text_input("You:", "Pls Write Your Something.......")
28
- if input_text.isalpha():
29
- st.write(text, 'string', )
30
- else:
31
- st.write('Please type in a string Only')
32
- return input_text
33
-
34
- user_input=get_text()
35
- response = load_answer(user_input)
36
-
37
- submit = st.button('Generate')
38
-
39
- #If generate button is clicked
40
- if submit:
41
-
42
- st.subheader("Answer:")
43
-
44
- st.write(response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)