Spaces:
Sleeping
Sleeping
gera
commited on
Commit
·
ba4f426
1
Parent(s):
213365f
Infinite changes. Not working as I like, I can't make it flow as desired
Browse files
app.py
CHANGED
|
@@ -1,21 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
-
import
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
api_key =
|
| 7 |
client = OpenAI(api_key=api_key)
|
| 8 |
|
| 9 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
history_openai_format = []
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
response = client.chat.completions.create(
|
| 21 |
model='gpt-4-turbo',
|
|
@@ -29,12 +56,47 @@ def chat(message, history):
|
|
| 29 |
partial_message = partial_message + chunk.choices[0].delta.content
|
| 30 |
yield partial_message
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
auth=os.environ.get("APP_USERS", "null")
|
| 39 |
-
auth=json.loads(auth)
|
| 40 |
demo.launch(auth=auth)
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
+
import tiktoken
|
| 4 |
+
from os import getenv as os_getenv
|
| 5 |
+
from json import loads as json_loads
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import fitz
|
| 8 |
|
| 9 |
+
api_key = os_getenv("OPENAI_APIKEY")
|
| 10 |
client = OpenAI(api_key=api_key)
|
| 11 |
|
| 12 |
+
def get_prompt(books, question = None):
|
| 13 |
+
prompt = (
|
| 14 |
+
f"Read the following books.\n" +
|
| 15 |
+
f"Each book may have some pages at the beggining with data about the book, an index, or table of content, etc. " +
|
| 16 |
+
f"Pages may have a header and/or a footer. Consider all this maybe present." +
|
| 17 |
+
f"Please answer all below in the suggested format, in the language of the book:\n"+
|
| 18 |
+
f"**Title**: ...\n"
|
| 19 |
+
f"**Author**: ...\n"
|
| 20 |
+
f"**Chapter Names**: ...\n"
|
| 21 |
+
f"**Characters**: \n"
|
| 22 |
+
f"**Detailed Summary**: \n"
|
| 23 |
+
)
|
| 24 |
+
prompt += f"{books}\n"
|
| 25 |
+
|
| 26 |
+
return prompt
|
| 27 |
+
|
| 28 |
+
def chat(message, history, files):
|
| 29 |
history_openai_format = []
|
| 30 |
+
|
| 31 |
+
if len(history) == 0:
|
| 32 |
+
raise gr.Error("Primero hay que subir un libro")
|
| 33 |
+
|
| 34 |
+
if len(history) == 1:
|
| 35 |
+
if message:
|
| 36 |
+
raise gr.Error("First message must be empty")
|
| 37 |
+
message = history[0][0]
|
| 38 |
+
else:
|
| 39 |
+
for human, assistant in history:
|
| 40 |
+
if human:
|
| 41 |
+
history_openai_format.append({"role": "user", "content": human })
|
| 42 |
+
if assistant:
|
| 43 |
+
history_openai_format.append({"role": "assistant", "content":assistant})
|
| 44 |
+
|
| 45 |
+
history_openai_format.append({"role": "user", "content": message})
|
| 46 |
|
| 47 |
response = client.chat.completions.create(
|
| 48 |
model='gpt-4-turbo',
|
|
|
|
| 56 |
partial_message = partial_message + chunk.choices[0].delta.content
|
| 57 |
yield partial_message
|
| 58 |
|
| 59 |
+
def get_text(filename):
|
| 60 |
+
answer = ""
|
| 61 |
+
suffix = Path(filename).suffix
|
| 62 |
+
if suffix in [".pdf"]:
|
| 63 |
+
for i,page in enumerate(fitz.open(filename)):
|
| 64 |
+
answer += f"\n### Page #{i+1}\n{page.get_text()}\n"
|
| 65 |
+
elif suffix in [".txt"]:
|
| 66 |
+
answer = open(filename).read()
|
| 67 |
+
return answer
|
| 68 |
+
|
| 69 |
+
def files_ready(filenames):
|
| 70 |
+
encoder = encoding = tiktoken.encoding_for_model('gpt-4-turbo')
|
| 71 |
+
answer = ''
|
| 72 |
+
for i, name in enumerate(filenames):
|
| 73 |
+
answer += f"\n## Document #{i+1}\nName: {Path(name).name}\n"
|
| 74 |
+
answer += get_text(name)
|
| 75 |
+
|
| 76 |
+
return len(encoder.encode(answer)), [[get_prompt(answer), None]]
|
| 77 |
+
|
| 78 |
+
def files_changed(filenames):
|
| 79 |
+
if not filenames:
|
| 80 |
+
return 0
|
| 81 |
+
|
| 82 |
+
with gr.Blocks(title="Book summarization and more") as demo:
|
| 83 |
+
with gr.Row():
|
| 84 |
+
files = gr.Files(file_types=["txt","doc","docx","pdf"] )
|
| 85 |
+
tokens = gr.Text("0", label="Tokens")
|
| 86 |
+
|
| 87 |
+
chat = gr.ChatInterface(
|
| 88 |
+
fn=chat,
|
| 89 |
+
title="Summarization and more",
|
| 90 |
+
additional_inputs=[files],
|
| 91 |
+
multimodal=False)
|
| 92 |
+
|
| 93 |
+
other = gr.Button(interactive=False)
|
| 94 |
+
files.upload(files_ready, [files], [tokens, chat.chatbot_state])
|
| 95 |
+
files.change(files_changed, files, tokens)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
auth=os_getenv("APP_USERS", "null")
|
| 99 |
+
auth=json_loads(auth)
|
| 100 |
|
|
|
|
|
|
|
| 101 |
demo.launch(auth=auth)
|
| 102 |
+
|