File size: 986 Bytes
90ba62d
 
 
 
0e66e48
9dc66e6
c012ec0
0e66e48
 
 
 
90ba62d
 
 
0e66e48
 
 
90ba62d
c012ec0
90ba62d
 
 
c012ec0
90ba62d
 
 
 
 
 
 
 
 
 
 
 
0e66e48
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 gradio as gr
import openai
import os

# Set up OpenAI API key
api_key = os.getenv("OPENAI_API_KEY")

if not api_key:
    print("Warning: OPENAI_API_KEY is not set. Please add it in your Hugging Face Space secrets.")
else:
    openai.api_key = api_key  # Set the API key globally

def chat_with_ai(prompt):
    """Generates a response from OpenAI's GPT-3.5."""
    if not api_key:
        return "Error: Missing API key. Please configure it in the Hugging Face Space settings."

    try:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": prompt}]
        )
        return response["choices"][0]["message"]["content"]
    except Exception as e:
        return f"Error: {str(e)}"

# Create Gradio interface
iface = gr.Interface(
    fn=chat_with_ai,
    inputs=gr.Textbox(placeholder="Ask me anything..."),
    outputs=gr.Textbox()
)

# Launch the app
if __name__ == "__main__":
    iface.launch()