Jofthomas HF staff commited on
Commit
daee091
1 Parent(s): 4d16de9

Add application file

Browse files
Files changed (1) hide show
  1. app.py +159 -0
app.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import json
4
+ import gradio as gr
5
+ from langchain import HuggingFaceHub
6
+ from langchain.output_parsers import PydanticOutputParser
7
+ from langchain import PromptTemplate
8
+ from pydantic import BaseModel, Field
9
+ from transformers import pipeline
10
+ from typing import List
11
+ import openai
12
+
13
+
14
+
15
+ openai.api_key = os.environ['OPENAI_API_TOKEN']
16
+
17
+ HUGGINGFACEHUB_API_TOKEN=os.environ["HUGGINGFACEHUB_API_TOKEN"]
18
+
19
+
20
+ classifier = pipeline("sentiment-analysis")
21
+ repo_id = "tiiuae/falcon-7b-instruct"
22
+ llm = HuggingFaceHub(huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
23
+ repo_id=repo_id,
24
+ model_kwargs={"temperature":0.1, "max_new_tokens":300})
25
+
26
+
27
+
28
+
29
+ class Sentiment(BaseModel):
30
+ label: str = Field(description="Is a the above rview sentiment 'Good', or 'Bad' ?")
31
+
32
+
33
+
34
+
35
+ def sentence_builder(Model,Text):
36
+ if Model=="Sentiment analysis pipeline":
37
+ good_label,bad_label=pipeline_sentiment(Text)
38
+ if Model=="Falcon-7b-instruct":
39
+ good_label,bad_label=falcon_sentiment(Text)
40
+ if Model=="GPT-4 Function call":
41
+ good_label,bad_label=gpt4_sentiment(Text)
42
+
43
+ print({"Good": good_label, "Bad": bad_label})
44
+ return {"Good": good_label, "Bad": bad_label}
45
+
46
+
47
+ demo = gr.Interface(
48
+ sentence_builder,
49
+ [
50
+
51
+ gr.Dropdown(
52
+ ["Sentiment analysis pipeline","Falcon-7b-instruct","GPT-4 Function call"], label="Model", info="Wich model to use"
53
+ ),
54
+ gr.Textbox(
55
+ label="Text",
56
+ info="Review text",
57
+ lines=2,
58
+ value="I'm not sure about the origin of this product, it seems suspicious.",
59
+ ),
60
+
61
+ ],
62
+ "label",
63
+ examples=[
64
+
65
+ ["Sentiment analysis pipeline","The product broke ! Great ..."],
66
+ ["Sentiment analysis pipeline","Not sure if I like it or not."],
67
+ ["Sentiment analysis pipeline","This product is just a toy."],
68
+ ["Sentiment analysis pipeline","Bought a TV, received an Ipad..."],
69
+ ["Sentiment analysis pipeline","Could have found the same on wish.com ."],
70
+ ["Sentiment analysis pipeline","They did a wonderfull job at ripping us."],
71
+ ["Sentiment analysis pipeline","Is it dropshipping ?"],
72
+ ]
73
+ )
74
+
75
+ def pipeline_sentiment(text):
76
+ out = classifier(text)
77
+ print(out)
78
+ if out[0]['label'] == "NEGATIVE":
79
+ bad_label = out[0]['score']
80
+ good_label = 1 - bad_label
81
+ elif out[0]['label'] == "POSITIVE":
82
+ good_label = out[0]['score']
83
+ bad_label = 1 - good_label
84
+ print(good_label, bad_label)
85
+ return good_label, bad_label
86
+
87
+ def falcon_sentiment(text):
88
+
89
+ parser = PydanticOutputParser(pydantic_object=Sentiment)
90
+
91
+ prompt = PromptTemplate(
92
+ template="Classify the following review as Good or Bad : .\n{format_instructions}\n{query}\n",
93
+ input_variables=["query"],
94
+ partial_variables={"format_instructions": parser.get_format_instructions()},
95
+ )
96
+
97
+ _input = prompt.format_prompt(query=text)
98
+
99
+ output = llm(_input.to_string())
100
+ print('Sentiment :', output)
101
+ try:
102
+ parsed_output=parser.parse(output)
103
+ print("parsed_output",parsed_output)
104
+ except:
105
+
106
+ pattern = r'\b(Good|Bad)\b'
107
+ match = re.search(pattern, output)
108
+ print(match)
109
+ print(match.group(0))
110
+ parsed_output=match.group(0)
111
+ return parsed_output=="Good",parsed_output=='Bad'
112
+
113
+ def Find_sentiment(sentence):
114
+
115
+ system_msg = {"role": "system", "content": f'You an AI that help me label sentences based on multiples features'}
116
+
117
+ # Initialize messages array
118
+ messages = [system_msg]
119
+ message=f"Sentence: {sentence}, based on the sentence, fin the best sentiment to discribe this sentence."
120
+
121
+ messages.append({"role": "user", "content": message})
122
+ try:
123
+ response = openai.ChatCompletion.create(
124
+ model="gpt-4-0613",
125
+ messages=messages,
126
+ functions=[
127
+ {
128
+ "name": "set_sentiment",
129
+ "description": "Set the sentiment of the sentence.",
130
+ "parameters": {
131
+ "type": "object",
132
+ "properties": {
133
+ "sentiment": {
134
+ "type": "string",
135
+ "description": "A Sentiment between 'Good' or 'Bad'",
136
+ },
137
+
138
+ },
139
+ "required": ["sentiment"],
140
+ },
141
+ }
142
+ ],
143
+ function_call={"name": "set_sentiment"},
144
+ )
145
+ assistant_msg = response['choices'][0]['message']
146
+ response_options = assistant_msg.to_dict()['function_call']['arguments']
147
+ options = json.loads(response_options)
148
+
149
+ return options["sentiment"]
150
+ except openai.error.OpenAIError as e:
151
+ print("Some error happened here.",e)
152
+ return
153
+
154
+ def gpt4_sentiment(text):
155
+ out=Find_sentiment(text)
156
+ return out=="Good",out=='Bad'
157
+ if __name__ == "__main__":
158
+
159
+ demo.launch()