Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,28 +5,30 @@ import os
|
|
| 5 |
import io
|
| 6 |
import nltk
|
| 7 |
import openai
|
| 8 |
-
import
|
| 9 |
-
import sys
|
| 10 |
-
import subprocess
|
| 11 |
-
|
| 12 |
-
# install required libraries
|
| 13 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
|
| 14 |
-
|
| 15 |
-
# download required NLTK data packages
|
| 16 |
-
nltk.download('punkt')
|
| 17 |
-
nltk.download('all')
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
# Put your OpenAI API key here
|
| 28 |
openai.api_key = os.getenv('OpenAPI')
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def pdf_to_text(file, user_prompt):
|
| 31 |
z = zipfile.ZipFile(file.name, 'r')
|
| 32 |
texts = []
|
|
@@ -46,27 +48,23 @@ def pdf_to_text(file, user_prompt):
|
|
| 46 |
chunk = tokens[i:i + 2000]
|
| 47 |
chunk_str = ' '.join(chunk)
|
| 48 |
# Using OpenAI API
|
| 49 |
-
response =
|
| 50 |
-
|
| 51 |
-
messages=[
|
| 52 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
| 53 |
-
{"role": "user", "content": user_prompt},
|
| 54 |
-
{"role": "user", "content": chunk_str},
|
| 55 |
-
]
|
| 56 |
-
)
|
| 57 |
-
texts.append(response['choices'][0]['message']['content'])
|
| 58 |
else:
|
| 59 |
# Using OpenAI API
|
| 60 |
-
response =
|
| 61 |
-
|
| 62 |
-
messages=[
|
| 63 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
| 64 |
-
{"role": "user", "content": user_prompt},
|
| 65 |
-
{"role": "user", "content": text},
|
| 66 |
-
]
|
| 67 |
-
)
|
| 68 |
-
texts.append(response['choices'][0]['message']['content'])
|
| 69 |
return '\n'.join(texts)
|
| 70 |
|
| 71 |
-
iface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
iface.launch(share=False)
|
|
|
|
|
|
| 5 |
import io
|
| 6 |
import nltk
|
| 7 |
import openai
|
| 8 |
+
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Put your OpenAI API key here
|
| 11 |
openai.api_key = os.getenv('OpenAPI')
|
| 12 |
|
| 13 |
+
def call_openai_api(prompt):
|
| 14 |
+
max_retries = 3
|
| 15 |
+
for attempt in range(max_retries):
|
| 16 |
+
try:
|
| 17 |
+
response = openai.ChatCompletion.create(
|
| 18 |
+
model="gpt-3.5-turbo",
|
| 19 |
+
messages=[
|
| 20 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 21 |
+
{"role": "user", "content": prompt},
|
| 22 |
+
]
|
| 23 |
+
)
|
| 24 |
+
return response['choices'][0]['message']['content']
|
| 25 |
+
except Exception as e:
|
| 26 |
+
if attempt < max_retries - 1: # if it's not the last attempt
|
| 27 |
+
time.sleep(1) # wait for 1 seconds before retrying
|
| 28 |
+
continue
|
| 29 |
+
else:
|
| 30 |
+
return str(e) # return the exception message after the last attempt
|
| 31 |
+
|
| 32 |
def pdf_to_text(file, user_prompt):
|
| 33 |
z = zipfile.ZipFile(file.name, 'r')
|
| 34 |
texts = []
|
|
|
|
| 48 |
chunk = tokens[i:i + 2000]
|
| 49 |
chunk_str = ' '.join(chunk)
|
| 50 |
# Using OpenAI API
|
| 51 |
+
response = call_openai_api(chunk_str)
|
| 52 |
+
texts.append(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
else:
|
| 54 |
# Using OpenAI API
|
| 55 |
+
response = call_openai_api(text)
|
| 56 |
+
texts.append(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
return '\n'.join(texts)
|
| 58 |
|
| 59 |
+
iface = gr.Interface(
|
| 60 |
+
fn=pdf_to_text,
|
| 61 |
+
inputs=[
|
| 62 |
+
gr.inputs.File(label="PDF File", description="Upload a Zip file containing ONLY PDF files from which the knowledge will be extracted."),
|
| 63 |
+
gr.inputs.Textbox(label="User Prompt", description="Enter a prompt to guide the AI's responses.")
|
| 64 |
+
],
|
| 65 |
+
outputs=gr.outputs.Textbox(label="Extracted Text", description="Cognitive Agent response from the AI."),
|
| 66 |
+
title="PDF Text Extractor",
|
| 67 |
+
description="This Cognitive Agent allows you to prompt a corpus knowledge, uploaded as a single Zip file, using OpenAI's GPT-3 model."
|
| 68 |
+
)
|
| 69 |
iface.launch(share=False)
|
| 70 |
+
|