File size: 1,019 Bytes
77bc84a
dd87744
93f9b19
dd87744
77bc84a
93f9b19
a96ad77
77bc84a
dd87744
 
 
 
 
 
75af692
77bc84a
93f9b19
75af692
dd87744
 
75af692
dd87744
75af692
77bc84a
2432747
dd87744
77bc84a
dd87744
 
77bc84a
 
 
dd87744
 
77bc84a
 
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
import os
import gradio as gr
from openai import OpenAI

# Load API key from Hugging Face secret (set in the Secrets tab)
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# Define the chat function
def chat_with_gpt(message, history):
    messages = []
    for human, assistant in history:
        messages.append({"role": "user", "content": human})
        messages.append({"role": "assistant", "content": assistant})
    messages.append({"role": "user", "content": message})

    # Call OpenAI API
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        max_tokens=150,
        temperature=0.7
    )

    # Return the assistant's reply
    return response.choices[0].message.content

# Create Gradio interface
demo = gr.ChatInterface(
    fn=chat_with_gpt,
    title="ChatGPT Demo on Hugging Face",
    examples=["What's the capital of Canada?", "Tell me a joke.", "Explain quantum computing."],
    cache_examples=False
)

# Launch the app
demo.launch()