Spaces:
Sleeping
Sleeping
gera
commited on
Commit
·
c407aa4
1
Parent(s):
3ef6533
first versions, quite advanced
Browse files- app.py +145 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai.types.beta.threads import Message, MessageDelta
|
| 2 |
+
from os import getenv as os_getenv, path as os_path
|
| 3 |
+
from time import sleep
|
| 4 |
+
from json import loads as json_loads
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import openai
|
| 7 |
+
|
| 8 |
+
OPENAI_API_KEY = os_getenv('OPENAI_API_KEY')
|
| 9 |
+
|
| 10 |
+
client = openai.OpenAI(api_key=OPENAI_API_KEY)
|
| 11 |
+
|
| 12 |
+
assistant_id = "asst_NHnYFIdpvioacAJqWYMchJHI"
|
| 13 |
+
vector_id = "vs_sqT4VRRTwkH7JPr3AT8CpoXV"
|
| 14 |
+
|
| 15 |
+
class EventHandler(openai.AssistantEventHandler):
|
| 16 |
+
# def on_event(self, event):
|
| 17 |
+
# print(f"event: {event.event}\n{event.data}\n\n")
|
| 18 |
+
|
| 19 |
+
def on_tool_call_created(self, tool_call):
|
| 20 |
+
print(f"\nassistant > {tool_call.type}\n", flush=True)
|
| 21 |
+
|
| 22 |
+
def on_tool_call_delta(self, delta, snapshot):
|
| 23 |
+
if delta.type == 'code_interpreter':
|
| 24 |
+
if delta.code_interpreter.input:
|
| 25 |
+
print(delta.code_interpreter.input, end="", flush=True)
|
| 26 |
+
if delta.code_interpreter.outputs:
|
| 27 |
+
print(f"\n\noutput >", flush=True)
|
| 28 |
+
for output in delta.code_interpreter.outputs:
|
| 29 |
+
if output.type == "logs":
|
| 30 |
+
print(f"\n{output.logs}", flush=True)
|
| 31 |
+
|
| 32 |
+
def chat(user_message, history, state):
|
| 33 |
+
if not state['user']:
|
| 34 |
+
gr.Warning("You need to authenticate first")
|
| 35 |
+
yield
|
| 36 |
+
else:
|
| 37 |
+
thread = state['thread']
|
| 38 |
+
if thread is None:
|
| 39 |
+
thread = client.beta.threads.create(
|
| 40 |
+
tool_resources={
|
| 41 |
+
"file_search": {
|
| 42 |
+
"vector_store_ids": [vector_id]
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
)
|
| 46 |
+
state['thread'] = thread
|
| 47 |
+
|
| 48 |
+
client.beta.threads.messages.create(
|
| 49 |
+
thread_id=thread.id,
|
| 50 |
+
role="user",
|
| 51 |
+
content=user_message,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
with client.beta.threads.runs.stream(
|
| 55 |
+
thread_id=thread.id,
|
| 56 |
+
assistant_id=assistant_id,
|
| 57 |
+
event_handler=EventHandler(),
|
| 58 |
+
) as stream:
|
| 59 |
+
total = ''
|
| 60 |
+
for delta in stream.text_deltas:
|
| 61 |
+
total += delta
|
| 62 |
+
yield total
|
| 63 |
+
|
| 64 |
+
def chat_nostream(user_message, history, state):
|
| 65 |
+
if state['user'] is None:
|
| 66 |
+
return
|
| 67 |
+
|
| 68 |
+
thread = state['thread']
|
| 69 |
+
if thread is None:
|
| 70 |
+
thread = client.beta.threads.create(
|
| 71 |
+
tool_resources={
|
| 72 |
+
"file_search": {
|
| 73 |
+
"vector_store_ids": [vector_id]
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
)
|
| 77 |
+
state['thread'] = thread
|
| 78 |
+
|
| 79 |
+
# Add the user's message to the thread
|
| 80 |
+
client.beta.threads.messages.create(
|
| 81 |
+
thread_id=thread.id,
|
| 82 |
+
role="user",
|
| 83 |
+
content=user_message,
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Run the Assistant
|
| 87 |
+
run = client.beta.threads.runs.create(thread_id=thread.id,
|
| 88 |
+
assistant_id=assistant_id)
|
| 89 |
+
|
| 90 |
+
while True:
|
| 91 |
+
run_status = client.beta.threads.runs.retrieve(thread_id=thread.id,
|
| 92 |
+
run_id=run.id)
|
| 93 |
+
print(f"Run status: {run_status.status}")
|
| 94 |
+
if run_status.status == 'completed':
|
| 95 |
+
break
|
| 96 |
+
sleep(5)
|
| 97 |
+
|
| 98 |
+
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
| 99 |
+
response = messages.data[0].content[0].text.value
|
| 100 |
+
|
| 101 |
+
yield response
|
| 102 |
+
|
| 103 |
+
def new_state():
|
| 104 |
+
return gr.State({
|
| 105 |
+
"user": None,
|
| 106 |
+
"thread": None,
|
| 107 |
+
})
|
| 108 |
+
|
| 109 |
+
def auth(token, state):
|
| 110 |
+
tokens=os_getenv("APP_TOKENS", None)
|
| 111 |
+
if tokens is None:
|
| 112 |
+
state["user"] = "anonymous"
|
| 113 |
+
else:
|
| 114 |
+
tokens=json_loads(tokens)
|
| 115 |
+
state["user"] = tokens.get(token, None)
|
| 116 |
+
return "", state
|
| 117 |
+
|
| 118 |
+
AUTH_JS = """function auth_js(token, state) {
|
| 119 |
+
if (!!document.location.hash) {
|
| 120 |
+
token = document.location.hash
|
| 121 |
+
document.location.hash=""
|
| 122 |
+
}
|
| 123 |
+
return [token, state]
|
| 124 |
+
}
|
| 125 |
+
"""
|
| 126 |
+
with gr.Blocks(title="Dr. Luis Chiozza - Medicina y Psicoanalisis") as demo:
|
| 127 |
+
state = new_state()
|
| 128 |
+
gr.ChatInterface(
|
| 129 |
+
chat,
|
| 130 |
+
title="Dr. Luis Chiozza - Medicina y Psicoanalisis",
|
| 131 |
+
description="Habla con la colección de Medicina y Psicoanalisis del Dr. Luis Chiozza",
|
| 132 |
+
additional_inputs=[state],
|
| 133 |
+
examples=[["Que diferencias hay entre el cuerpo y el Alma?"]],
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
token = gr.Textbox(visible=False)
|
| 137 |
+
demo.load(auth,
|
| 138 |
+
[token,state],
|
| 139 |
+
[token,state],
|
| 140 |
+
js=AUTH_JS)
|
| 141 |
+
|
| 142 |
+
demo.launch(
|
| 143 |
+
height=700,
|
| 144 |
+
allowed_paths=["."])
|
| 145 |
+
# auth_dependency=authenticate) #auth=authenticate)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
openai
|