Spaces:
Runtime error
Runtime error
import gradio as gr | |
import yaml | |
import openai | |
from pathlib import Path | |
# Load settings | |
settings_path = Path(__file__).parent / "settings.yaml" | |
with open(settings_path, "r") as f: | |
settings = yaml.safe_load(f) | |
# Configure OpenAI client | |
openai.api_key = settings["openai_api_key"] | |
openai.api_base = settings["openai_base_url"] | |
def chatbot(message, history): | |
messages = [{"role": "system", "content": "You are a helpful assistant answering question concerning Air Force Personnel Center material. You also try your best to cite the source of your answer."}] | |
for human, assistant in history: | |
messages.append({"role": "user", "content": human}) | |
messages.append({"role": "assistant", "content": assistant}) | |
messages.append({"role": "user", "content": message}) | |
client = openai.OpenAI( | |
api_key=settings["openai_api_key"], | |
base_url=settings["openai_base_url"] | |
) | |
response = client.chat.completions.create( | |
model=settings["openai_model"], | |
messages=messages, | |
temperature=0 | |
) | |
return response.choices[0].message.content | |
demo = gr.ChatInterface( | |
fn=chatbot, | |
examples=["Hello!", "How are you?", "What's the weather like?"], | |
title="AFPC Fine-Tuned Chatbot", | |
description="AFPC Fine-Tuned Chatbot", | |
) | |
if __name__ == "__main__": | |
demo.launch() |