File size: 5,539 Bytes
c407aa4
 
 
 
d6df1ce
 
c407aa4
4baeaea
 
c407aa4
d6df1ce
 
 
 
 
90693de
c407aa4
278b485
c407aa4
 
 
 
 
 
 
 
d6df1ce
c407aa4
 
 
 
 
 
 
 
 
d6df1ce
b138a26
 
 
d6df1ce
 
 
 
 
c407aa4
 
 
d6df1ce
c407aa4
 
 
 
 
 
 
b816bdf
 
278b485
b816bdf
 
 
 
 
 
 
 
c407aa4
b816bdf
 
c407aa4
b816bdf
 
 
 
 
 
c407aa4
b816bdf
 
 
c407aa4
b816bdf
 
 
 
 
 
 
c407aa4
b816bdf
 
c407aa4
b816bdf
c407aa4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6df1ce
61e85cd
 
278b485
 
61e85cd
 
d6df1ce
4baeaea
 
61e85cd
278b485
4baeaea
 
 
 
278b485
4baeaea
 
 
 
 
 
 
 
 
 
 
 
 
fed8b6a
4baeaea
 
 
 
 
278b485
 
 
 
 
4baeaea
 
 
 
 
 
 
c407aa4
 
 
fed8b6a
21099ff
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from os import getenv as os_getenv, path as os_path
from time import sleep
from json import loads as json_loads
import gradio as gr
from openai import OpenAI
client = OpenAI()

assistant_id = os_getenv("OPENAI_ASSISTANT_ID")
vector_id = os_getenv("OPENAI_VECTOR_ID") 

ANS_SHORT = "Resumida"
ANS_REGULAR = "Normal"
ANS_LONG = "Detallada"

def chat(user_message, history, state, long_or_short):
    if (state is None) or (not state['user']):
        gr.Warning("You need to authenticate first")
        yield "You need to authenticate first"
    else:
        thread = state['thread']
        if thread is None:
            thread = client.beta.threads.create(
                tool_resources={
                    "file_search": {
                        "vector_store_ids": [vector_id]
                    }
                },
            )
            state['thread'] = thread

        client.beta.threads.messages.create(
            thread_id=thread.id,
            role="user",
            content=user_message,
        )

        details = ""
        if state["user"] != "anonymous":
            details += f"User's name is {state['user']}."

        if   long_or_short == ANS_SHORT:
            details = "Please make a complete but short answer"
        elif long_or_short == ANS_LONG:
            details = "Please make complete and detailed answer. Long is better than short. Use tables and lists if you need it"

        with client.beta.threads.runs.stream(
            thread_id=thread.id,
            assistant_id=assistant_id,
            additional_instructions=details,
        ) as stream:
            total = ''
            for delta in stream.text_deltas:
                total += delta
                yield total

def chat_nostream(user_message, history, state):
    if (state is None) or (not state['user']):
        gr.Warning("You need to authenticate first")
        yield "You need to authenticate first"
    else:
        thread = state['thread']
        if thread is None:
            thread = client.beta.threads.create(
                tool_resources={
                    "file_search": {
                        "vector_store_ids": [vector_id]
                    }
                }
            )
            state['thread'] = thread

        # Add the user's message to the thread
        client.beta.threads.messages.create(
            thread_id=thread.id,
            role="user",
            content=user_message,
        )

        # Run the Assistant
        run = client.beta.threads.runs.create(thread_id=thread.id,
                                            assistant_id=assistant_id)

        while True:
            run_status = client.beta.threads.runs.retrieve(thread_id=thread.id,
                                                            run_id=run.id)
            print(f"Run status: {run_status.status}")
            if run_status.status == 'completed':
                break
            sleep(5)

        messages = client.beta.threads.messages.list(thread_id=thread.id)
        response = messages.data[0].content[0].text.value

        yield response

def new_state():
    return gr.State({
        "user": None,
        "thread": None,
    })

def auth(token, state):
    tokens=os_getenv("APP_TOKENS", None)
    if tokens is None:
        state["user"] = "anonymous"
    else:
        tokens=json_loads(tokens)
        state["user"] = tokens.get(token, None)
    return "", state

AUTH_JS = """function auth_js(token, state) {
    if (!!document.location.hash) {
        token = document.location.hash
        document.location.hash=""
    }        
    return [token, state]
}
"""

theme = gr.Theme.from_hub("freddyaboulton/dracula_revamped@0.3.9")
theme.set(
    color_accent_soft="#818eb6",            # ChatBot.svelte / .message-row.panel.user-row
    background_fill_secondary="#6272a4",    # ChatBot.svelte / .message-row.panel.bot-row
    button_primary_text_color="*button_secondary_text_color",
    button_primary_background_fill="*button_secondary_background_fill")

with gr.Blocks(
    title="Dr. Luis Chiozza - Medicina y Psicoanalisis",
    fill_height=True,
    theme=theme) as demo:
        state = new_state()

        gr.HTML("""
            <h1>Dr. Luis Chiozza - Medicina y Psicoanalisis</h1>
            <p>Habla con la colección de Libros de Medicina y Psicoanalisis del Dr. Luis Chiozza</p>
        """)
        with gr.Row(variant="compact"):
            gr.HTML("Largo de la respuesta")
            long_or_short = gr.Radio(
                choices = [ANS_SHORT, ANS_REGULAR, ANS_LONG],
                value = ANS_REGULAR,
                show_label=False,
                container=False,
                label = "Largo de la respuesta",
                scale=3)

        gr.ChatInterface(
            chat,
            chatbot=gr.Chatbot(show_label=False, render=False, layout = "panel", scale=1),
            additional_inputs=[state, long_or_short],
            examples=[
                ["Qué diferencias hay entre el cuerpo y el Alma?"],
                ["Cuáles son las distintas funciones de los órganos del cuerpo y su relación con el alma?"],
            ],
            submit_btn="Enviar",
            stop_btn="Detener",
            undo_btn=None,
            clear_btn="Borrar",
            retry_btn=None
        )

        token = gr.Textbox(visible=False)
        demo.load(auth,
            [token,state],
            [token,state],
            js=AUTH_JS)

demo.launch(
    height=700,
    show_api=False,
    allowed_paths=["."])