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