File size: 1,335 Bytes
90e7c08
 
 
 
 
 
c72493f
 
021d0fe
 
c72493f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
021d0fe
 
c72493f
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
import openai

openai.api_key = "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
"


import os
import openai
import gradio as gr

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

def chat_with_synthia(user_message, chat_history):
    messages = [{"role": "system", "content": "You are SYNTHIA, a helpful and friendly AI assistant."}]
    for user, bot in chat_history:
        messages.append({"role": "user", "content": user})
        messages.append({"role": "assistant", "content": bot})
    messages.append({"role": "user", "content": user_message})

    response = openai.ChatCompletion.create(
        model="gpt-4o-mini",
        messages=messages,
        max_tokens=150,
        temperature=0.7,
    )
    reply = response.choices[0].message['content']
    chat_history.append((user_message, reply))
    return "", chat_history

with gr.Blocks() as app:
    gr.Markdown("# SYNTHIA β€” Your smart AI assistant πŸ€–πŸ’–")
    chatbot = gr.Chatbot()
    msg = gr.Textbox(placeholder="Say something to SYNTHIA...")
    clear = gr.Button("Clear")

    msg.submit(chat_with_synthia, [msg, chatbot], [msg, chatbot])
    clear.click(lambda: None, None, chatbot, queue=False)

if __name__ == "__main__":
    app.launch()