Spaces:
Sleeping
Sleeping
Xiaoyue Wang commited on
Commit ·
83b6e89
1
Parent(s): 58b3852
Update LLM
Browse files
app.py
CHANGED
|
@@ -1,40 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
-
import
|
|
|
|
| 5 |
|
|
|
|
| 6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
def respond(
|
| 10 |
-
|
| 11 |
-
history: list[tuple[str, str]]
|
| 12 |
-
):
|
| 13 |
messages = [{"role": "system", "content": "Evaluate the flight turn data"}]
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 20 |
-
|
| 21 |
messages.append({"role": "user", "content": message})
|
| 22 |
-
|
| 23 |
response = openai.chat.completions.create(
|
| 24 |
model="ft:gpt-4o-2024-08-06:personal::BAk7EUnu",
|
| 25 |
messages=messages
|
| 26 |
)
|
| 27 |
-
|
| 28 |
-
yield reply
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 33 |
-
"""
|
| 34 |
-
demo = gr.ChatInterface(
|
| 35 |
-
respond
|
| 36 |
-
)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
+
import random
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
+
# Set OpenAI API key
|
| 8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
| 10 |
+
def generate_random_turn_data():
|
| 11 |
+
"""Generates random flight turn data."""
|
| 12 |
+
data_points = []
|
| 13 |
+
for point in range(10):
|
| 14 |
+
data_points.append({
|
| 15 |
+
"Point": point * 36,
|
| 16 |
+
"ASI": random.randint(85, 100), # Airspeed Indicator in knots
|
| 17 |
+
"AI": "level",
|
| 18 |
+
"Heading": random.randint(0, 359), # Heading in degrees
|
| 19 |
+
"Turn and Slip": random.choice(["turn needle left", "turn needle right", "centered"]),
|
| 20 |
+
"Bank Angle": random.choice([-45, -40, -35, 35, 40, 45]),
|
| 21 |
+
"VSI": random.choice([-100, -50, 0, 50, 100]) # Vertical Speed Indicator in feet per minute
|
| 22 |
+
})
|
| 23 |
+
|
| 24 |
+
# Format data for display
|
| 25 |
+
input_blocks = ["\n".join([f"{key}: {value}" for key, value in point.items()]) for point in data_points]
|
| 26 |
+
return "\n\n".join(input_blocks)
|
| 27 |
|
| 28 |
+
def respond(message, history: list[tuple[str, str]]):
|
| 29 |
+
"""Handles chat responses using OpenAI."""
|
|
|
|
|
|
|
| 30 |
messages = [{"role": "system", "content": "Evaluate the flight turn data"}]
|
| 31 |
+
for user_msg, assistant_msg in history:
|
| 32 |
+
if user_msg:
|
| 33 |
+
messages.append({"role": "user", "content": user_msg})
|
| 34 |
+
if assistant_msg:
|
| 35 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
| 36 |
messages.append({"role": "user", "content": message})
|
| 37 |
+
|
| 38 |
response = openai.chat.completions.create(
|
| 39 |
model="ft:gpt-4o-2024-08-06:personal::BAk7EUnu",
|
| 40 |
messages=messages
|
| 41 |
)
|
| 42 |
+
return response.choices[0].message.content
|
|
|
|
| 43 |
|
| 44 |
+
def get_random_turn_data():
|
| 45 |
+
"""Returns randomly generated flight turn data."""
|
| 46 |
+
return generate_random_turn_data()
|
| 47 |
|
| 48 |
+
demo = gr.ChatInterface(respond)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
demo = gr.Blocks()
|
| 51 |
+
with demo:
|
| 52 |
+
gr.Markdown("# Flight Turn Data Evaluator")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
user_input = gr.Textbox(label="Enter Flight Turn Data or Generate Random Data")
|
| 55 |
+
with gr.Row():
|
| 56 |
+
submit_button = gr.Button("Submit")
|
| 57 |
+
random_button = gr.Button("Random Generate")
|
| 58 |
+
with gr.Row():
|
| 59 |
+
output_display = gr.Textbox(label="Response", interactive=False)
|
| 60 |
+
|
| 61 |
+
submit_button.click(respond, inputs=[user_input, gr.State([])], outputs=output_display)
|
| 62 |
+
random_button.click(get_random_turn_data, inputs=[], outputs=user_input)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
demo.launch()
|