Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 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 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message")
|
| 50 |
-
],
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Set your OpenAI API key
|
| 6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
|
| 8 |
+
def chatbot(prompt):
|
| 9 |
+
try:
|
| 10 |
+
response = openai.ChatCompletion.create(
|
| 11 |
+
model="gpt-3.5-turbo",
|
| 12 |
+
messages=[{"role": "system", "content": "You are a helpful assistant."},
|
| 13 |
+
{"role": "user", "content": prompt}]
|
| 14 |
+
)
|
| 15 |
+
return response["choices"][0]["message"]["content"]
|
| 16 |
+
except Exception as e:
|
| 17 |
+
return str(e)
|
| 18 |
+
|
| 19 |
+
# Create a Gradio interface
|
| 20 |
+
iface = gr.Interface(fn=chatbot,
|
| 21 |
+
inputs="text",
|
| 22 |
+
outputs="text",
|
| 23 |
+
title="AI Chatbot",
|
| 24 |
+
description="Ask me anything!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
if __name__ == "__main__":
|
| 27 |
+
iface.launch()
|