Spaces:
Sleeping
Sleeping
karthikeyan-r
commited on
Commit
•
d0fec0d
1
Parent(s):
fa04c4e
Create llm.py
Browse files
llm.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import AzureOpenAI
|
2 |
+
import docx2txt
|
3 |
+
import PyPDF2
|
4 |
+
import os
|
5 |
+
import pdfplumber
|
6 |
+
from pdfminer.high_level import extract_text
|
7 |
+
|
8 |
+
class OpenaiAPI:
|
9 |
+
def __init__(self) -> None:
|
10 |
+
self.client = AzureOpenAI(
|
11 |
+
api_key=os.getenv("API"),
|
12 |
+
api_version="2023-07-01-preview",
|
13 |
+
azure_endpoint=os.getenv("URL"),
|
14 |
+
)
|
15 |
+
|
16 |
+
def get_response(self, prompt) -> str:
|
17 |
+
try:
|
18 |
+
completion = self.client.chat.completions.create(
|
19 |
+
model="GPT-4o", # e.g., gpt-35-instant
|
20 |
+
messages=prompt,
|
21 |
+
temperature=0,
|
22 |
+
)
|
23 |
+
return completion.choices[0].message.content
|
24 |
+
except Exception as e:
|
25 |
+
print(f"An error occurred while generating prompt from OpenAI API: {e}")
|
26 |
+
|
27 |
+
def docx_to_text(self, docx_path):
|
28 |
+
text = docx2txt.process(docx_path)
|
29 |
+
return text
|
30 |
+
|
31 |
+
def pdf_to_text_pypdf2(self, pdf_file):
|
32 |
+
text = extract_text(pdf_file)
|
33 |
+
return text
|
34 |
+
|
35 |
+
def save_uploadedfile(self, uploadedfile):
|
36 |
+
with open(os.path.join("tempDir", uploadedfile), "wb") as f:
|
37 |
+
f.write(uploadedfile.getbuffer())
|
38 |
+
return st.success(f"Saved File: {uploadedfile.name} to tempDir")
|