Spaces:
Sleeping
Sleeping
File size: 2,560 Bytes
ac7d7e6 5932030 ac7d7e6 5932030 5b6ddca 5932030 ac7d7e6 5b6ddca 5932030 5b6ddca ac7d7e6 5b6ddca 5932030 5b6ddca 5932030 5b6ddca ac7d7e6 5b6ddca ac7d7e6 ba8dc4b 5b6ddca ac7d7e6 5932030 5b6ddca 5932030 ac7d7e6 5b6ddca ba8dc4b 5b6ddca 3456b2c d2c6cd8 5b6ddca b34e565 5b6ddca ba8dc4b 5b6ddca 0a25159 5b6ddca ac7d7e6 |
1 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import gradio as gr
import openai
import os
from typing import Generator
openai.api_key = os.getenv("OPENAI_API_KEY")
BASE_SYSTEM_MESSAGE = "You are a pilot assistant. You should provide concise information about the questions asked."
MAX_TOKENS = 1024
def get_system_message(aircraft: str) -> str:
return f"{BASE_SYSTEM_MESSAGE} You are specifically knowledgeable about the {aircraft} aircraft."
def generate_response(message, history, aircraft):
messages = [{"role": "system", "content": get_system_message(aircraft)}]
for user_msg, assistant_msg in history:
messages.append({"role": "user", "content": user_msg})
messages.append({"role": "assistant", "content": assistant_msg})
messages.append({"role": "user", "content": message})
response = openai.chat.completions.create(
model="gpt-4o",
messages=messages,
max_tokens=MAX_TOKENS,
stream=False,
)
return response.choices[0].message.content
def update_chat_history(history):
return "\n".join(
f"User: {msg[0]}\nAssistant: {msg[1]}" for msg in history
)
def handle_submit(user_input, aircraft, history):
if not user_input.strip():
return "", history, "Please enter a question."
reply = generate_response(user_input, history, aircraft)
history.append((user_input, reply))
updated_history = update_chat_history(history)
return "", history, updated_history
with gr.Blocks() as demo:
with gr.Row():
gr.Markdown("# Avia Agent", elem_id="title")
restart_btn = gr.Button("Restart", scale=0.2, elem_classes=["small-button"])
with gr.Row():
aircraft = gr.Dropdown(
choices=["C172", "Cirrus SR20/SR22"],
value="C172",
label="Select Aircraft"
)
with gr.Row():
chat_history_display = gr.Textbox(
label="Conversation History",
lines=3,
interactive=False
)
with gr.Row():
user_input = gr.Textbox(
label="Ask a question",
placeholder="How to do steep turns?",
lines=2,
)
submit_btn = gr.Button("Send", scale=0.1, elem_classes=["small-button"])
conversation_state = gr.State([])
submit_btn.click(
handle_submit,
inputs=[user_input, aircraft, conversation_state],
outputs=[user_input, conversation_state, chat_history_display]
)
restart_btn.click(None, None, None, js="() => { window.location.reload() }")
if __name__ == "__main__":
demo.launch()
|