tensorgirl commited on
Commit
a98760a
1 Parent(s): 68214a6

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +27 -62
utils.py CHANGED
@@ -6,40 +6,15 @@ import io
6
  import pandas as pd
7
  import os
8
  import torch
9
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
10
- from transformers import pipeline
11
- from openai import OpenAI
12
- from groq import Groq
13
- import time
14
  import json
15
  from openai import OpenAI
16
 
17
  openai_key = "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"
18
- print(openai_key)
19
  client = OpenAI(api_key = openai_key)
20
  desc = pd.read_excel('Descriptor.xlsx',header = None)
21
  desc_list = desc.iloc[:,0].to_list()
22
 
23
- def callAzure(prompt,text):
24
-
25
- url = "https://Meta-Llama-3-70B-Instruct-fkqip-serverless.eastus2.inference.ai.azure.com"
26
- api_key = "o5yaLhTIvg0s5zuYVInBpyneEZO8oonY"
27
- client = OpenAI(base_url=url, api_key=api_key)
28
- msg = "{} {}".format(prompt, text)
29
-
30
- response = client.chat.completions.create(
31
- messages=[
32
- {
33
- "role": "user",
34
- "content": msg,
35
- }
36
- ],
37
- model="azureai",
38
- max_tokens = 1000
39
- )
40
-
41
- return response.choices[0].message.content
42
-
43
  def filter(input_json):
44
 
45
  sym = pd.read_excel('symbol.xlsx',header = None)
@@ -67,16 +42,16 @@ def filter(input_json):
67
  return [1, document]
68
 
69
  def summary(input_json):
70
-
71
  prompt = pd.read_excel('DescriptorPrompt.xlsx')
72
  promptShort = prompt.iloc[:,1].to_list()
73
- promptLong = prompt.iloc[:,2].to_list()
74
-
75
  output = {}
76
  filtering_results = filter(input_json)
77
  if filtering_results[0] == 0:
78
- #return 0
79
- return filtering_results[1]
80
 
81
  id = desc_list.index(input_json['Descriptor'])
82
  long_text = filtering_results[1]
@@ -84,58 +59,48 @@ def summary(input_json):
84
  long_text = long_text.rstrip()
85
 
86
  long_text = long_text[:6000]
87
-
88
  url = 'https://www.bseindia.com/xml-data/corpfiling/AttachLive/'+ input_json['FileURL'].split('Pname=')[-1]
89
 
90
  output["Link to BSE website"] = url
91
 
92
  output["Date of time of receiving data from BSE"] = input_json["newsdate"] + "Z"
93
-
94
- output["Stock Ticker"] = input_json['symbol']
95
-
96
- answer = callAzure("You are an financial expert. Wherever possible, mention the name of the company " + promptShort[id] + " Do not exceed over 400 characters", long_text)
97
- try:
98
- idx = answer.index("\n")
99
- except:
100
- idx = -2
101
- output['Short Summary'] = answer[idx+2:]
102
-
103
- answer = callAzure("Make sure the following summary of a news article is not more than 80 words. Rewrite it and make it below 80 words ", output['Short Summary'])
104
- try:
105
- idx = answer.index("\n")
106
- except:
107
- idx = -2
108
- output['Short Summary'] = answer[idx+2:]
109
-
110
- prompt = "Provide the main topic of the news article strictly as a tag, using only one or two words, with only the first word capitalized and the rest in lowercase. No additional text or explanation."
111
- answer = callAzure(prompt, output['Short Summary'])
112
- output['Tag'] = answer
113
 
114
- prompt = "Generate a precise headline for the news article that includes the name of the company. Be very careful about correctly representing any financial figures mentioned in lakhs and crores. Provide only the headline, with no additional text or explanation."
115
- answer = callAzure(prompt, output['Short Summary'])
116
- output['Headline'] = answer
117
 
118
  utc_now = datetime.datetime.utcnow()
119
  ist_now = utc_now.astimezone(datetime.timezone(datetime.timedelta(hours=5, minutes=30)))
120
-
121
  Date = ist_now.strftime("%Y-%m-%d")
122
  time = ist_now.strftime("%X")
123
  output['Date and time of data delivery from Skylark'] = Date+"T"+time+"Z"
124
-
125
- prompt = "Answer in one word the sentiment of this News out of Positive, Negative or Neutral {}"
126
- output['Sentiment'] = callAzure(prompt, output['Short Summary'])
 
 
 
 
 
 
 
127
 
128
  completion = client.chat.completions.create(
129
  model="gpt-4o",
130
  messages=[
131
- {"role": "system", "content": "You are a financial expert. Help the client with summarizing the financial newsletter. Write the summary in max 500 words. Do not truncate."},
132
- {"role": "user", "content": "{} {}".format(promptLong[id], long_text)}
133
  ],
134
  temperature=0,
135
  max_tokens=4000,
136
  )
137
 
138
- output['Long summary'] = completion.choices[0].message.content
 
 
 
 
 
139
 
140
  # response = client.images.generate(
141
  # model="dall-e-3",
 
6
  import pandas as pd
7
  import os
8
  import torch
9
+ import time
 
 
 
 
10
  import json
11
  from openai import OpenAI
12
 
13
  openai_key = "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"
 
14
  client = OpenAI(api_key = openai_key)
15
  desc = pd.read_excel('Descriptor.xlsx',header = None)
16
  desc_list = desc.iloc[:,0].to_list()
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  def filter(input_json):
19
 
20
  sym = pd.read_excel('symbol.xlsx',header = None)
 
42
  return [1, document]
43
 
44
  def summary(input_json):
45
+
46
  prompt = pd.read_excel('DescriptorPrompt.xlsx')
47
  promptShort = prompt.iloc[:,1].to_list()
48
+ promptLong = prompt.iloc[:,2].to_list()
49
+
50
  output = {}
51
  filtering_results = filter(input_json)
52
  if filtering_results[0] == 0:
53
+ return 0
54
+ #return filtering_results[1]
55
 
56
  id = desc_list.index(input_json['Descriptor'])
57
  long_text = filtering_results[1]
 
59
  long_text = long_text.rstrip()
60
 
61
  long_text = long_text[:6000]
62
+
63
  url = 'https://www.bseindia.com/xml-data/corpfiling/AttachLive/'+ input_json['FileURL'].split('Pname=')[-1]
64
 
65
  output["Link to BSE website"] = url
66
 
67
  output["Date of time of receiving data from BSE"] = input_json["newsdate"] + "Z"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ output["Stock Ticker"] = input_json['symbol']
 
 
70
 
71
  utc_now = datetime.datetime.utcnow()
72
  ist_now = utc_now.astimezone(datetime.timezone(datetime.timedelta(hours=5, minutes=30)))
73
+
74
  Date = ist_now.strftime("%Y-%m-%d")
75
  time = ist_now.strftime("%X")
76
  output['Date and time of data delivery from Skylark'] = Date+"T"+time+"Z"
77
+
78
+ prompt = """
79
+ Return the output in json format. This is the financial article {}
80
+ Following are the keys of the json.
81
+ 1. Short Summary - {} Do not exceed over 400 characters. Make sure it is no more than 80 words
82
+ 2. Tag - Provide the main topic of the news article strictly as a tag, using only one or two words, with only the first word capitalized and the rest in lowercase. No additional text or explanation.
83
+ 3. Headline - Generate a precise headline for the news article that includes the name of the company. Be very careful about correctly representing any financial figures mentioned in lakhs and crores. Provide only the headline, with no additional text or explanation.
84
+ 4. Sentiment - Answer in one word the sentiment of this News out of Positive, Negative or Neutral.
85
+ 5. Long summary - {} Write the summary in max 500 words.
86
+ """.format(long_text,promptShort[id],promptLong[id])
87
 
88
  completion = client.chat.completions.create(
89
  model="gpt-4o",
90
  messages=[
91
+ {"role": "system", "content": "You are a financial expert. Help the client with summarizing the financial newsletter. Do not truncate."},
92
+ {"role": "user", "content": "{}".format(prompt)}
93
  ],
94
  temperature=0,
95
  max_tokens=4000,
96
  )
97
 
98
+ answer = json.loads(completion.choices[0].message.content[8:-3])
99
+ output['Short Summary'] = answer['Short Summary']
100
+ output['Tag'] = answer['Tag']
101
+ output['Headline'] = answer['Headline']
102
+ output['Sentiment'] = answer['Sentiment']
103
+ output['Long summary'] = answer['Long summary']
104
 
105
  # response = client.images.generate(
106
  # model="dall-e-3",