move demo functions out of model_server (#67)

* pending

* remove

* fix docker build
This commit is contained in:
Adil Hafeez 2024-09-20 14:38:10 -07:00 committed by GitHub
parent ca5c9e4824
commit 31f26ef7ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 122 additions and 50 deletions

View file

@ -1,6 +1,7 @@
import os
from openai import OpenAI
import gradio as gr
import logging as log
from dotenv import load_dotenv
load_dotenv()
@ -9,6 +10,8 @@ OPEN_API_KEY=os.getenv("OPENAI_API_KEY")
CHAT_COMPLETION_ENDPOINT = os.getenv("CHAT_COMPLETION_ENDPOINT")
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-3.5-turbo")
log.info("CHAT_COMPLETION_ENDPOINT: ", CHAT_COMPLETION_ENDPOINT)
client = OpenAI(api_key=OPEN_API_KEY, base_url=CHAT_COMPLETION_ENDPOINT)
def predict(message, history):
@ -17,16 +20,20 @@ def predict(message, history):
# history_openai_format.append({"role": "user", "content": human })
# history_openai_format.append({"role": "assistant", "content":assistant})
history.append({"role": "user", "content": message})
log.info("CHAT_COMPLETION_ENDPOINT: ", CHAT_COMPLETION_ENDPOINT)
log.info("history: ", history)
try:
response = client.chat.completions.create(model='gpt-3.5-turbo',
response = client.chat.completions.create(model=MODEL_NAME,
messages= history,
temperature=1.0
)
except Exception as e:
print(e)
log.info(e)
# remove last user message in case of exception
history.pop()
log.info("CHAT_COMPLETION_ENDPOINT: ", CHAT_COMPLETION_ENDPOINT)
log.info("Error with OpenAI API: {}".format(e.message))
raise gr.Error("Error with OpenAI API: {}".format(e.message))
# for chunk in response:
@ -52,4 +59,4 @@ with gr.Blocks(fill_height=True, css="footer {visibility: hidden}") as demo:
txt.submit(predict, [txt, state], [chatbot, state])
demo.launch(server_name="0.0.0.0", server_port=8080, show_error=True)
demo.launch(server_name="0.0.0.0", server_port=8080, show_error=True, debug=True)