Spaces:
Runtime error
Runtime error
Commit ·
96f748e
1
Parent(s): b959451
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import os
|
|
@@ -5,36 +39,42 @@ import os
|
|
| 5 |
# Setup and initialization
|
| 6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
-
from openai import OpenAI
|
| 9 |
-
|
| 10 |
-
client = OpenAI(openai.api_key=os.getenv("OPENAI_API_KEY"))
|
| 11 |
-
|
| 12 |
def openai_chat(prompt, chat_history):
|
| 13 |
"""Generic function to handle chatting with OpenAI's GPT model."""
|
| 14 |
try:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
iface = gr.Interface(
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
iface.launch(share=True)
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
-
|
| 38 |
# import gradio as gr
|
| 39 |
# import openai
|
| 40 |
# import os
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
# import openai
|
| 3 |
+
# import os
|
| 4 |
+
|
| 5 |
+
# # Setup and initialization
|
| 6 |
+
# openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
|
| 8 |
+
# from openai import OpenAI
|
| 9 |
+
|
| 10 |
+
# client = OpenAI(openai.api_key=os.getenv("OPENAI_API_KEY"))
|
| 11 |
+
|
| 12 |
+
# def openai_chat(prompt, chat_history):
|
| 13 |
+
# """Generic function to handle chatting with OpenAI's GPT model."""
|
| 14 |
+
# try:
|
| 15 |
+
# response = client.engines.gpt_3_5_turbo.completions.create(
|
| 16 |
+
# prompt=prompt,
|
| 17 |
+
# max_tokens=150
|
| 18 |
+
# )
|
| 19 |
+
# bot_message = response.choices[0].text.strip()
|
| 20 |
+
# chat_history.append({"role": "assistant", "content": bot_message})
|
| 21 |
+
# return '', chat_history
|
| 22 |
+
# except Exception as e:
|
| 23 |
+
# return f"An error occurred: {str(e)}", chat_history
|
| 24 |
+
|
| 25 |
+
# iface = gr.Interface(
|
| 26 |
+
# fn=chatbot_response,
|
| 27 |
+
# inputs="text",
|
| 28 |
+
# outputs="text",
|
| 29 |
+
# title="Chatbot",
|
| 30 |
+
# description="Ask a question and get an answer from the chatbot."
|
| 31 |
+
# )
|
| 32 |
+
|
| 33 |
+
# iface.launch(share=True)
|
| 34 |
+
|
| 35 |
import gradio as gr
|
| 36 |
import openai
|
| 37 |
import os
|
|
|
|
| 39 |
# Setup and initialization
|
| 40 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def openai_chat(prompt, chat_history):
|
| 43 |
"""Generic function to handle chatting with OpenAI's GPT model."""
|
| 44 |
try:
|
| 45 |
+
response = openai.ChatCompletion.create(
|
| 46 |
+
model="gpt-3.5-turbo",
|
| 47 |
+
messages=[
|
| 48 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 49 |
+
{"role": "user", "content": prompt}
|
| 50 |
+
],
|
| 51 |
+
max_tokens=150
|
| 52 |
+
)
|
| 53 |
+
bot_message = response.choices[0].message['content']
|
| 54 |
+
chat_history.append({"role": "assistant", "content": bot_message})
|
| 55 |
+
return '', chat_history
|
| 56 |
except Exception as e:
|
| 57 |
+
return f"An error occurred: {str(e)}", chat_history
|
| 58 |
+
|
| 59 |
+
def chatbot_response(prompt, chat_history):
|
| 60 |
+
"""Handles the chat functionality."""
|
| 61 |
+
response, chat_history = openai_chat(prompt, chat_history)
|
| 62 |
+
return response, chat_history
|
| 63 |
|
| 64 |
+
# Gradio Interface Layout
|
| 65 |
iface = gr.Interface(
|
| 66 |
+
fn=chatbot_response,
|
| 67 |
+
inputs=gr.inputs.Textbox(lines=7, label="Chat with AI"),
|
| 68 |
+
outputs=gr.outputs.Textbox(label="Reply"),
|
| 69 |
+
title="AI Chatbot",
|
| 70 |
+
description="Ask anything you want",
|
| 71 |
+
theme="compact"
|
| 72 |
)
|
| 73 |
|
| 74 |
iface.launch(share=True)
|
| 75 |
|
| 76 |
|
| 77 |
|
|
|
|
| 78 |
# import gradio as gr
|
| 79 |
# import openai
|
| 80 |
# import os
|