shivakerur99 commited on
Commit
d8ddb5b
1 Parent(s): e0b222b

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +251 -0
main.py ADDED
@@ -0,0 +1,251 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+ from pydantic import BaseModel
3
+ from fastapi import FastAPI, HTTPException, File, UploadFile
4
+ from datetime import datetime
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+ from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String
7
+ from databases import Database
8
+ from textblob import TextBlob
9
+
10
+ import whisperx
11
+ import gc
12
+
13
+
14
+ import nltk
15
+ from nltk.tokenize import word_tokenize
16
+ from nltk.corpus import stopwords
17
+ import openai
18
+ import time
19
+ nltk.download('punkt')
20
+ nltk.download('stopwords')
21
+
22
+ openai.api_key = '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'
23
+
24
+ app = FastAPI()
25
+
26
+ import os
27
+
28
+ import requests
29
+ import json
30
+
31
+ # Set up CORS (Cross-Origin Resource Sharing) for allowing requests from all origins
32
+ origins = ["*"]
33
+ app.add_middleware(
34
+ CORSMiddleware,
35
+ allow_origins=origins,
36
+ allow_credentials=True,
37
+ allow_methods=["GET", "POST", "PUT", "DELETE"],
38
+ allow_headers=["*"],
39
+ )
40
+
41
+ # Define SQLAlchemy engine and metadata
42
+ DATABASE_URL = "sqlite:///./test.db"
43
+ engine = create_engine(DATABASE_URL)
44
+ metadata = MetaData()
45
+
46
+ # Define the document table schema
47
+ documents = Table(
48
+ "documents",
49
+ metadata,
50
+ Column("id", Integer, primary_key=True),
51
+ Column("filename", String),
52
+ Column("upload_date", String),
53
+ Column("content", String),
54
+ )
55
+
56
+ # Create the document table in the database
57
+ metadata.create_all(engine)
58
+
59
+ # Define Pydantic model for the document
60
+ class Document(BaseModel):
61
+ filename: str
62
+ upload_date: str
63
+ content: str
64
+
65
+ # Initialize database connection pool
66
+ database = Database(DATABASE_URL)
67
+
68
+
69
+
70
+ def analyze_sentiment(text):
71
+ blob = TextBlob(text)
72
+ sentiment = blob.sentiment.polarity
73
+ if sentiment > 0:
74
+ return "positive"
75
+ elif sentiment < 0:
76
+ return "negative"
77
+ else:
78
+ return "neutral"
79
+
80
+ def analyze_conversation_sentiment(conversation):
81
+ sentiment_analysis = {}
82
+ for line in conversation:
83
+ speaker, dialogue = line.strip().split(':')
84
+ sentiment = analyze_sentiment(dialogue)
85
+ sentiment_analysis[line] = sentiment
86
+ return sentiment_analysis
87
+
88
+
89
+ def parse_conversation(content):
90
+ return content.strip().split('\n')
91
+
92
+
93
+ def extract_active_words(text):
94
+ tokens = word_tokenize(text)
95
+ stop_words = set(stopwords.words('english'))
96
+ active_words = [word for word in tokens if word.isalnum() and word.lower() not in stop_words]
97
+ return active_words
98
+
99
+
100
+ def generate_description(speaker, sentiment, active_words):
101
+ prompt = f"{speaker}: Sentiment: {sentiment}\nActive Words: {', '.join(active_words)}\nDescription:"
102
+ response = openai.Completion.create(
103
+ engine="gpt-3.5-turbo-instruct",
104
+ prompt=prompt+"do not mention sentiment and active words in description, In output based on sentiment get psychological insights derived from the conversation, some insights about speakers. Please don’t provide summary of conversation, key words, etc. Output should be related to sentimental analysis.",
105
+ temperature=0.7,
106
+ max_tokens=len(speaker) + 50 # Adjusted to a fixed value for simplicity
107
+ )
108
+ return response.choices[0].text.strip()
109
+
110
+ # Endpoint for uploading text or mp3 or wav files
111
+
112
+ device = "cpu"
113
+ batch_size = 1 # reduce if low on GPU mem
114
+ compute_type = "int8" # change to "int8" if low on GPU mem (may reduce accuracy)
115
+ audio_file = "hello.wav"
116
+
117
+
118
+
119
+
120
+ @app.post("/upload/")
121
+ async def upload_text_file(file: UploadFile = File(...)):
122
+ # Check if the uploaded file is a text file
123
+ if not file.filename.lower().endswith(('.txt', '.mp3', '.wav')):
124
+ raise HTTPException(status_code=400, detail="Only text files (TXT) or mp3 or wav are allowed.")
125
+
126
+ # Define the file path to save the uploaded file in the current directory
127
+ # file_path = os.path.join(os.getcwd(), file.filename)
128
+
129
+ # Define the file path to save the uploaded file in the current directory
130
+ file_path = os.path.join(os.getcwd(), file.filename)
131
+
132
+ # Save the uploaded file
133
+ with open(file_path, "wb") as f:
134
+ content = await file.read() # Read the content of the uploaded file asynchronously
135
+ f.write(content)
136
+
137
+ if file.filename.lower().endswith('.txt'):
138
+ # Read the content of the file asynchronously
139
+ contentinitial = await file.read()
140
+ contentlast = contentinitial.decode('utf-8')
141
+ filtered_content = '\n'.join(line for line in contentlast.splitlines() if line.strip())
142
+ content = filtered_content
143
+ print(content)
144
+
145
+ elif file.filename.lower().endswith((".mp3", ".wav")):
146
+ # Save the uploaded audio file in the current directory
147
+ audio_file = file_path
148
+ model = whisperx.load_model("base", device, compute_type=compute_type)
149
+ audio = whisperx.load_audio(audio_file)
150
+ result = model.transcribe(audio, batch_size=batch_size)
151
+ model_a, metadata = whisperx.load_align_model(language_code=result["language"],
152
+ device=device)
153
+
154
+ result = whisperx.align(result["segments"], model_a,
155
+ metadata,
156
+ audio,
157
+ device,
158
+ return_char_alignments=False)
159
+
160
+ # print(result["segments"]) # after alignment
161
+
162
+
163
+ diarize_model = whisperx.DiarizationPipeline(use_auth_token=HF_TOKEN, device=device)
164
+
165
+ # add min/max number of speakers if known
166
+ diarize_segments = diarize_model(audio)
167
+ # diarize_model(audio, min_speakers=min_speakers, max_speakers=max_speakers)
168
+
169
+ result = whisperx.assign_word_speakers(diarize_segments, result)
170
+ # print(diarize_segments)
171
+ # print(result["segments"])
172
+ full_transcript=""
173
+ for segment in result["segments"]:
174
+ speaker_id = segment["speaker"]
175
+ transcript = segment["text"]
176
+ full_transcript += f'[{speaker_id}] {transcript}\n'
177
+ content=full_transcript
178
+
179
+
180
+
181
+ # Create document object
182
+ doc = Document(filename=file.filename, upload_date=str(datetime.now()), content=content)
183
+
184
+ # Insert the document data into the database
185
+ async with database.transaction():
186
+ query = documents.insert().values(
187
+ filename=doc.filename,
188
+ upload_date=doc.upload_date,
189
+ content=doc.content
190
+ )
191
+ last_record_id = await database.execute(query)
192
+
193
+ return doc
194
+
195
+ class DataInput(BaseModel):
196
+ responseData: str
197
+
198
+ @app.post("/doc/")
199
+ async def process_data(data: DataInput):
200
+ # Access responseData and userInput
201
+ content = data.responseData
202
+ conversation = parse_conversation(content)
203
+ sentiments_with_active_words = []
204
+
205
+
206
+ #IMPORTANT KINDLY READ IT:
207
+ #IMPORTANT KINDLY READ IT:
208
+ #IMPORTANT KINDLY READ IT:
209
+ #IMPORTANT KINDLY READ IT:
210
+ #********************OpenAI sentiment analysis part which takes to many api request calls to process big files *****************************#
211
+ # for sentence in conversation:
212
+ # # Using OpenAI's sentiment analysis API
213
+ # result = openai.Completion.create(
214
+ # engine="gpt-3.5-turbo-instruct",
215
+ # prompt=sentence + " sentiment:",
216
+ # temperature=0,
217
+ # max_tokens=1,
218
+ # n=1,
219
+ # stop=None,
220
+ # )
221
+ # sentiment = result['choices'][0]['text'].strip()
222
+ # time.sleep(20)
223
+ # # Extract active words
224
+ # active_words = extract_active_words(sentence)
225
+
226
+
227
+
228
+ #********************sentiment analysis Using Textblob which is good and effecient and efficiency match with OpenAI's sentimental analysis********************#
229
+ sentiment_analysis = analyze_conversation_sentiment(conversation)
230
+ for line, sentiment in sentiment_analysis.items():
231
+ active_words = extract_active_words(line)
232
+ sentiments_with_active_words.append((sentiment, active_words))
233
+
234
+ # print(sentiments_with_active_words)
235
+ descriptions = []
236
+ for sentence, (sentiment, active_words) in zip(conversation, sentiments_with_active_words):
237
+ speaker = sentence.split(":")[0]
238
+ time.sleep(20) # Reduced sleep time for demonstration; adjust as per rate limits
239
+ description = generate_description(speaker, sentiment, active_words)
240
+ descriptions.append(description)
241
+
242
+
243
+ print("Generated Descriptions for each sentence:")
244
+ l=[]
245
+ for i, (sentence, description) in enumerate(zip(conversation, descriptions)):
246
+ l.append(f"Sentence {i+1}: {sentence}\n")
247
+ l.append(f"Description: {description}\n")
248
+
249
+
250
+ return l
251
+