File size: 946 Bytes
c73a7fe
21a1e5f
931f102
21a1e5f
 
 
43554f7
 
 
3d9e71b
43554f7
21a1e5f
 
43554f7
 
 
21a1e5f
43554f7
21a1e5f
43554f7
21a1e5f
43554f7
 
c73a7fe
21a1e5f
43554f7
 
3d9e71b
43554f7
931f102
 
21a1e5f
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
import os
import openai
import gradio as gr

openai.api_key = os.getenv("OPENAI_API_KEY")

def chat(user_input, history):
    if history is None:
        history = []

    # Call OpenAI
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role":"system","content":"You are a helpful dental assistant."}] +
                 [{"role":"user","content":m[0]} if i%2==0 else {"role":"assistant","content":m[1]} for i,m in enumerate(history)]
                 + [{"role":"user","content":user_input}]
    )
    
    reply = response["choices"][0]["message"]["content"]
    history.append((user_input, reply))

    # Keep last 6 exchanges
    return history[-6:], history[-6:]

demo = gr.Interface(
    fn=chat,
    inputs=[gr.Textbox(lines=2, placeholder="Ask a dental question"), gr.State()],
    outputs=[gr.Chatbot(label="Dental Assistant"), gr.State()],
    title="Dental Clinic Chatbot"
)

demo.launch()