Spaces:
Runtime error
Runtime error
Initial commit
Browse files- README.md +14 -13
- app.py +33 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
| 1 |
+
# GPT Chatbot
|
| 2 |
+
|
| 3 |
+
A simple web-based chatbot powered by OpenAI's GPT-3.5-turbo, built with Gradio and deployed on Hugging Face Spaces.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
- Friendly conversational UI
|
| 7 |
+
- Runs fully in the cloud
|
| 8 |
+
- Powered by OpenAI's API
|
| 9 |
+
|
| 10 |
+
## How to Run
|
| 11 |
+
This app is designed to run securely on Hugging Face Spaces. Make sure to set your `OPENAI_API_KEY` under the **Secrets** tab.
|
| 12 |
+
|
| 13 |
+
## Demo
|
| 14 |
+
[Click here to view the live demo](https://huggingface.co/spaces/jonowrenn/gpt-chatbot)
|
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Load your OpenAI API key from the Hugging Face Secrets
|
| 6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
|
| 8 |
+
def chat(user_input):
|
| 9 |
+
try:
|
| 10 |
+
messages = [
|
| 11 |
+
{"role": "system", "content": "You are a helpful and conversational chatbot."},
|
| 12 |
+
{"role": "user", "content": user_input}
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
response = openai.ChatCompletion.create(
|
| 16 |
+
model="gpt-3.5-turbo",
|
| 17 |
+
messages=messages
|
| 18 |
+
)
|
| 19 |
+
return response.choices[0].message["content"].strip()
|
| 20 |
+
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"Error: {e}"
|
| 23 |
+
|
| 24 |
+
# Gradio UI
|
| 25 |
+
interface = gr.Interface(
|
| 26 |
+
fn=chat,
|
| 27 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="GPT Chatbot",
|
| 30 |
+
description="A simple chatbot powered by OpenAI GPT-3.5. Enter a prompt and receive a response."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio
|