Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,28 +7,42 @@ import nltk
|
|
| 7 |
import openai
|
| 8 |
import time
|
| 9 |
|
| 10 |
-
import pip
|
| 11 |
-
import subprocess
|
| 12 |
-
import sys
|
| 13 |
-
|
| 14 |
# install required libraries
|
| 15 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
|
| 16 |
|
| 17 |
# download required NLTK data packages
|
| 18 |
nltk.download('punkt')
|
| 19 |
-
nltk.download('all') # or any other packages your project depends on
|
| 20 |
|
| 21 |
# Put your OpenAI API key here
|
| 22 |
openai.api_key = os.getenv('OpenAPI')
|
| 23 |
|
| 24 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
max_retries = 5
|
| 26 |
for attempt in range(max_retries):
|
| 27 |
try:
|
| 28 |
response = openai.ChatCompletion.create(
|
| 29 |
model="gpt-3.5-turbo",
|
| 30 |
messages=[
|
| 31 |
-
{"role": "system", "content": f"You are a knowledgeable assistant with a persona based on this: \n{
|
| 32 |
{"role": "user", "content": user_prompt},
|
| 33 |
]
|
| 34 |
)
|
|
@@ -51,20 +65,11 @@ def pdf_to_text(file, user_prompt):
|
|
| 51 |
text = ''
|
| 52 |
for page in pdf.pages:
|
| 53 |
text += page.extract_text()
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
chunk = tokens[i:i + 2000]
|
| 60 |
-
chunk_str = ' '.join(chunk)
|
| 61 |
-
# Using OpenAI API
|
| 62 |
-
response = call_openai_api(chunk_str, user_prompt)
|
| 63 |
-
texts.append(response)
|
| 64 |
-
else:
|
| 65 |
-
# Using OpenAI API
|
| 66 |
-
response = call_openai_api(text, user_prompt)
|
| 67 |
-
texts.append(response)
|
| 68 |
return '\n'.join(texts)
|
| 69 |
|
| 70 |
iface = gr.Interface(
|
|
|
|
| 7 |
import openai
|
| 8 |
import time
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# install required libraries
|
| 11 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
|
| 12 |
|
| 13 |
# download required NLTK data packages
|
| 14 |
nltk.download('punkt')
|
|
|
|
| 15 |
|
| 16 |
# Put your OpenAI API key here
|
| 17 |
openai.api_key = os.getenv('OpenAPI')
|
| 18 |
|
| 19 |
+
def create_persona(text):
|
| 20 |
+
max_retries = 5
|
| 21 |
+
for attempt in range(max_retries):
|
| 22 |
+
try:
|
| 23 |
+
response = openai.ChatCompletion.create(
|
| 24 |
+
model="gpt-3.5-turbo",
|
| 25 |
+
messages=[
|
| 26 |
+
{"role": "system", "content": "You are a knowledgeable assistant."},
|
| 27 |
+
{"role": "user", "content": text},
|
| 28 |
+
]
|
| 29 |
+
)
|
| 30 |
+
return response['choices'][0]['message']['content']
|
| 31 |
+
except Exception as e:
|
| 32 |
+
if attempt < max_retries - 1: # if it's not the last attempt
|
| 33 |
+
time.sleep(1) # wait for 1 seconds before retrying
|
| 34 |
+
continue
|
| 35 |
+
else:
|
| 36 |
+
return str(e) # return the exception message after the last attempt
|
| 37 |
+
|
| 38 |
+
def call_openai_api(persona, user_prompt):
|
| 39 |
max_retries = 5
|
| 40 |
for attempt in range(max_retries):
|
| 41 |
try:
|
| 42 |
response = openai.ChatCompletion.create(
|
| 43 |
model="gpt-3.5-turbo",
|
| 44 |
messages=[
|
| 45 |
+
{"role": "system", "content": f"You are a knowledgeable assistant with a persona based on this: \n{persona}"},
|
| 46 |
{"role": "user", "content": user_prompt},
|
| 47 |
]
|
| 48 |
)
|
|
|
|
| 65 |
text = ''
|
| 66 |
for page in pdf.pages:
|
| 67 |
text += page.extract_text()
|
| 68 |
+
# Create persona from text
|
| 69 |
+
persona = create_persona(text)
|
| 70 |
+
# Using OpenAI API
|
| 71 |
+
response = call_openai_api(persona, user_prompt)
|
| 72 |
+
texts.append(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return '\n'.join(texts)
|
| 74 |
|
| 75 |
iface = gr.Interface(
|