spxwlkr commited on
Commit
35a902c
·
verified ·
1 Parent(s): f9f0eb8

Upload 4 files

Browse files
AICodeInit/.DS_Store ADDED
Binary file (6.15 kB). View file
 
AICodeInit/LlamaCode.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from peft import AutoPeftModelForCausalLM
2
+ from transformers import AutoTokenizer, pipeline
3
+ # Load the model
4
+
5
+ def create_pipeline():
6
+ model = AutoPeftModelForCausalLM.from_pretrained("Moritz-Pfeifer/financial-times-classification-llama-2-7b-v1.3")
7
+ tokenizer = AutoTokenizer.from_pretrained("Moritz-Pfeifer/financial-times-classification-llama-2-7b-v1.3")
8
+ prompt = f"""
9
+ "You are given a news article regarding the greater Boston area.
10
+ Analyze the sentiment of the article enclosed in square brackets,
11
+ determine if it is positive, negative or neutral and return the answer as the corresponding sentiment label
12
+ "positive", "negative", or "neutral"".
13
+
14
+ """
15
+ pipe = pipeline(task="text-generation",
16
+ model=model,
17
+ tokenizer=tokenizer,
18
+ max_new_tokens = 1,
19
+ temperature = 0.1,
20
+ )
21
+ return prompt, pipe
22
+
23
+ def predict_text(text,pipe,prompt):
24
+ result = pipe((prompt+"\n"+'['+'{'+text+'}'+']'+' '+'='))
25
+ answer = result[0]['generated_text'].split("=")[-1]
26
+ if "positive" in answer.lower():
27
+ return "positive"
28
+ elif "negative" in answer.lower():
29
+ return "negative"
30
+ else:
31
+ return "neutral"
AICodeInit/__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ from .LlamaCode import *
2
+ from .spacy_textblob_functions import *
AICodeInit/spacy_textblob_functions.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from peft import AutoPeftModelForCausalLM
2
+ import spacy
3
+ import pandas as pd
4
+ from textblob import TextBlob
5
+
6
+
7
+ def load_model():
8
+ nlp = spacy.load("en_core_web_sm")
9
+ return nlp
10
+
11
+ def extract_entities(text,nlp):
12
+ doc = nlp(text)
13
+ entities = [(ent.text, ent.label_) for ent in doc.ents]
14
+ return entities
15
+
16
+ # Function to extract entities context
17
+ def extract_entities_with_context(text, nlp, window=5):
18
+ doc = nlp(text)
19
+ entity_context = []
20
+ for ent in doc.ents:
21
+ start = max(0, ent.start - window)
22
+ end = min(len(doc), ent.end + window)
23
+ context = doc[start:end].text
24
+ entity_context.append((ent.text, ent.label_, context))
25
+ return entity_context
26
+
27
+ def get_sentiment(text):
28
+ return TextBlob(text).sentiment.polarity
29
+
30
+ def analyze_entity_sentiments(entity_contexts):
31
+ sentiments = []
32
+ for text, label, context in entity_contexts:
33
+ sentiment = get_sentiment(context)
34
+ sentiments.append((text, label, sentiment))
35
+ return sentiments
36
+
37
+ def analyze_entity_sentiments_score(entity_contexts):
38
+ sentiments = []
39
+ for text, label, context in entity_contexts:
40
+ sentiment = get_sentiment(context)
41
+ sentiments.append((sentiment))
42
+ return sentiments
43
+
44
+ def calculate_avg_score(scores):
45
+ if scores:
46
+ return sum(scores) / len(scores)
47
+ else:
48
+ return float('inf')
49
+
50
+
51
+ def categorize_sentiment(score):
52
+ if score <= -0.1:
53
+ return 'Negative'
54
+ elif score >= 0.1:
55
+ return 'Positive'
56
+ else:
57
+ return 'Neutral'