File size: 789 Bytes
3d92239
 
b7cc39e
 
f296e45
3d92239
 
f296e45
 
 
3d92239
 
 
 
 
 
 
 
 
 
 
 
 
 
9b59479
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
import gradio as gr
import openai
import os

# Fetch OpenAI API Key from Hugging Face Secrets (NO .env file)
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

if OPENAI_API_KEY is None:
    raise ValueError("ERROR: OpenAI API key not found! Make sure to set it in Hugging Face Secrets.")

def chatbot(prompt):
    """ GPT-4o Mini Chatbot """
    openai.api_key = OPENAI_API_KEY
    response = openai.ChatCompletion.create(
        model="gpt-4o-mini",
        messages=[{"role": "system", "content": "You are a helpful assistant."},
                  {"role": "user", "content": prompt}]
    )
    return response['choices'][0]['message']['content']

# Create Gradio UI
app = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="GPT-4o Mini Chatbot")

# Run the chatbot
app.launch()