Spaces:
Paused
Paused
File size: 4,905 Bytes
6370672 |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import copy
import json
import os
import gradio as gr
import openai
from dotenv import load_dotenv
from gradio_pdf import PDF
from create_assistant import INSTRUCTIONS, MODEL
from thread import create_assistant_then_thread, render_markdown
load_dotenv()
OUTPUT_PATH = "data"
IMAGES_PATH = "images"
def fix_image_paths_in_thread(thread, base_path):
for tweet in thread:
for media in tweet.get("media"):
media["path"] = os.path.join(
"file", OUTPUT_PATH, os.path.basename(base_path), media["path"]
)
return thread
def run_create_thread(
url_or_path, openai_api_key, assistant_instructions, assistant_model
):
if not openai_api_key:
raise gr.Error("No OpenAI API Key provided.")
client = openai.OpenAI(api_key=openai_api_key)
try:
saved_path = create_assistant_then_thread(
url_or_path,
OUTPUT_PATH,
client,
assistant_kwargs={
"instructions": assistant_instructions,
"model": assistant_model,
},
)
except Exception as e:
raise gr.Error(e)
with open(os.path.join(saved_path, "processed_thread.json"), "r") as f:
thread = json.load(f)
fixed_thread = fix_image_paths_in_thread(copy.deepcopy(thread), saved_path)
thread_md = render_markdown(fixed_thread)
return (
thread_md,
json.dumps(thread, indent=2),
)
with gr.Blocks() as demo:
banner = gr.Markdown(
"""<div style="display: flex; align-items: center; justify-content: center; margin-top: 20px;">
<img src="file/images/logo.png" alt="ThreadGPT Logo" style="height: 60px; margin-right: 12px; margin-top: -12px;">
<h1 style="font-size: 48px">ThreadGPT</h1>
</div>
<p align="center" style="font-size: 12px;">π¨ Please be aware that usage of GPT-4 with the assistant API can incur high costs. Make sure to monitor your usage and understand the pricing details provided by OpenAI before proceeding. π¨
<br>
β There currently seems to be a bug with the Assistant API where a completed run returns no new messages from the assistant. If you encounter this, please click "Retry π". β</p>"""
)
with gr.Accordion("Configuration"):
with gr.Row():
api_key = gr.Textbox(
value=os.getenv("OPENAI_API_KEY"),
placeholder="sk-**************",
label="OpenAI API Key",
type="password",
interactive=True,
)
with gr.Column():
assistant_instr = gr.Textbox(
value=INSTRUCTIONS,
placeholder="Enter system instructions",
label="System Instructions",
interactive=True,
)
assistant_model = gr.Textbox(
value=MODEL,
placeholder="Enter model",
label="Model",
interactive=True,
)
with gr.Row():
url_or_path_state = gr.State("")
txt = gr.Textbox(
scale=6,
show_label=False,
placeholder="https://arxiv.org/pdf/1706.03762.pdf",
container=False,
)
upload_btn = gr.UploadButton("Upload PDF π", file_types=[".pdf"])
retry_btn = gr.Button("Retry π")
with gr.Row(visible=False) as output_row:
with gr.Column():
pdf = PDF(height=900)
with gr.Column():
with gr.Tab("Markdown"):
md_viewer = gr.Markdown()
with gr.Tab("JSON"):
json_viewer = gr.Textbox(lines=44)
txt.submit(
lambda url_or_path: ("", url_or_path, gr.Row(visible=True), "", ""),
[txt],
[txt, url_or_path_state, output_row, md_viewer, json_viewer],
).then(
lambda url_or_path: url_or_path,
[url_or_path_state],
[pdf],
).then(
run_create_thread,
[url_or_path_state, api_key, assistant_instr, assistant_model],
[md_viewer, json_viewer],
)
upload_btn.upload(
lambda path: (path, gr.Row(visible=True), "", ""),
[upload_btn],
[url_or_path_state, output_row, md_viewer, json_viewer],
).then(
lambda url_or_path: url_or_path,
[url_or_path_state],
[pdf],
).then(
run_create_thread,
[url_or_path_state, api_key, assistant_instr, assistant_model],
[md_viewer, json_viewer],
)
retry_btn.click(
lambda url_or_path: url_or_path,
[url_or_path_state],
[pdf],
).then(
run_create_thread,
[url_or_path_state, api_key, assistant_instr, assistant_model],
[md_viewer, json_viewer],
)
if __name__ == "__main__":
demo.launch(allowed_paths=[OUTPUT_PATH, IMAGES_PATH])
|