Alexander Hux commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Get the API key from the environment variable
|
| 10 |
+
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
openai.api_key = API_KEY
|
| 12 |
+
|
| 13 |
+
# Function to interact with OpenAI API
|
| 14 |
+
def chat_with_model(prompt):
|
| 15 |
+
if not API_KEY:
|
| 16 |
+
return "Error: API key not found. Please set it in the environment variables."
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
response = openai.ChatCompletion.create(
|
| 20 |
+
model="gpt-4", # Replace with your desired model
|
| 21 |
+
messages=[{"role": "user", "content": prompt}]
|
| 22 |
+
)
|
| 23 |
+
return response["choices"][0]["message"]["content"]
|
| 24 |
+
except openai.error.OpenAIError as e:
|
| 25 |
+
return f"Error: {str(e)}"
|
| 26 |
+
|
| 27 |
+
# Gradio Interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=chat_with_model,
|
| 30 |
+
inputs="text",
|
| 31 |
+
outputs="text",
|
| 32 |
+
title="ZEN AI Chatbot",
|
| 33 |
+
description="A simple chatbot powered by OpenAI's GPT models."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
iface.launch()
|