Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,44 @@
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Format chat history for display
|
| 27 |
-
chat_display = "\n".join([f"{m['role']}: {m['content']}" for m in chat_history])
|
| 28 |
-
|
| 29 |
-
return chat_display
|
| 30 |
-
|
| 31 |
-
iface = gr.Interface(
|
| 32 |
-
fn=generate_text,
|
| 33 |
-
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
|
| 34 |
-
outputs="text",
|
| 35 |
-
title="Gemini Pro Chatbot with History",
|
| 36 |
-
description="Ask me anything!",
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
iface.launch()
|
|
|
|
| 1 |
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv, find_dotenv
|
| 5 |
+
|
| 6 |
+
_ = load_dotenv(find_dotenv()) # read local .env file
|
| 7 |
+
|
| 8 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def get_completion_from_messages(messages,
|
| 12 |
+
model="gpt-3.5-turbo",
|
| 13 |
+
temperature=0,
|
| 14 |
+
max_tokens=500):
|
| 15 |
+
response = openai.chat.completions.create( # Use openai.chat.completions.create instead of openai.ChatCompletion.create
|
| 16 |
+
model=model,
|
| 17 |
+
messages=messages,
|
| 18 |
+
temperature=temperature,
|
| 19 |
+
max_tokens=max_tokens,
|
| 20 |
+
)
|
| 21 |
+
return response.choices[0].message.content
|
| 22 |
import gradio as gr
|
| 23 |
+
def generate(input, slider):
|
| 24 |
+
messages = [
|
| 25 |
+
{'role':'user',
|
| 26 |
+
'content': f"{input}"},
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
output = get_completion_from_messages(messages)
|
| 30 |
+
return output
|
| 31 |
+
|
| 32 |
+
demo = gr.ChatInterface(
|
| 33 |
+
fn=generate,
|
| 34 |
+
chatbot=gr.Chatbot(height=300),
|
| 35 |
+
textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7),
|
| 36 |
+
title="Your Helper CHAT Bot",
|
| 37 |
+
description="Ask 'Yes Man' any question",
|
| 38 |
+
theme="soft",
|
| 39 |
+
examples=["Hello", "Am I cool?", "Are tomatoes vegetables?"],
|
| 40 |
+
cache_examples=True,
|
| 41 |
+
#retry_btn=None,
|
| 42 |
+
#undo_btn="Delete Previous",
|
| 43 |
+
#clear_btn="Clear",
|
| 44 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|