Add prompt input

This commit is contained in:
Ethan 2024-12-26 14:33:07 -05:00
parent 56efe5fb5c
commit eb2a0d12c4
5 changed files with 76 additions and 47 deletions

View File

@ -1,9 +1,9 @@
use eframe::egui; use eframe::egui;
use model_response::ModelResponse;
use state::AppState; use state::AppState;
pub mod model_response; pub mod model_response;
pub mod model_selection; pub mod model_selection;
pub mod prompt_input;
pub mod state; pub mod state;
pub fn main() -> eframe::Result { pub fn main() -> eframe::Result {
@ -25,37 +25,18 @@ impl eframe::App for AppState {
ui.heading("Groq Model Comparison"); ui.heading("Groq Model Comparison");
ui.label("Compare Groq models with ease!"); ui.label("Compare Groq models with ease!");
ui.horizontal(|ui| {
let counter_color = match self.counter {
0 => egui::Color32::WHITE,
_ if self.counter > 0 => egui::Color32::GREEN,
_ => egui::Color32::RED,
};
ui.label("Counter:");
ui.label(egui::RichText::new(format!("{}", self.counter)).color(counter_color));
});
ui.horizontal(|ui| {
if ui.button("Increment").clicked() {
self.counter += 1;
}
if ui.button("Decrement").clicked() {
self.counter -= 1;
}
});
ui.add(model_selection::ModelSelection::new( ui.add(model_selection::ModelSelection::new(
self, self,
vec!["Model 1".to_string(), "Model 2".to_string()], vec!["Model 1".to_string(), "Model 2".to_string()],
)); ));
ui.add(ModelResponse::new( ui.add(prompt_input::PromptInput::new(self));
"This is a test message".to_string(),
200, ui.horizontal(|ui| {
100, for model in &self.selected_models {
)); ui.vertical(|ui| ui.add(model));
}
});
}); });
} }
} }

View File

@ -1,14 +1,16 @@
use eframe::egui::{self, Response, Ui, Widget}; use eframe::egui::{self, Response, Ui, Widget};
pub struct ModelResponse { pub struct ModelResponse {
pub name: String,
pub message: String, pub message: String,
pub status: i32, pub status: i32,
pub time: i32, pub time: i32,
} }
impl ModelResponse { impl ModelResponse {
pub fn new(message: String, status: i32, time: i32) -> Self { pub fn new(name: String, message: String, status: i32, time: i32) -> Self {
Self { Self {
name,
message, message,
status, status,
time, time,
@ -16,13 +18,16 @@ impl ModelResponse {
} }
} }
impl Widget for ModelResponse { impl Widget for &ModelResponse {
fn ui(self, ui: &mut Ui) -> Response { fn ui(self, ui: &mut Ui) -> Response {
egui::Frame::none() egui::Frame::none()
.inner_margin(8.0) .inner_margin(8.0)
.rounding(4.0) .rounding(4.0)
.fill(egui::Color32::DARK_GRAY) .fill(egui::Color32::DARK_GRAY)
.show(ui, |ui| { .show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(&self.name);
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Status:"); ui.label("Status:");
ui.label(self.status.to_string()); ui.label(self.status.to_string());
@ -32,13 +37,14 @@ impl Widget for ModelResponse {
ui.label("Time:"); ui.label("Time:");
ui.label(self.time.to_string()); ui.label(self.time.to_string());
}); });
});
egui::Frame::none() egui::Frame::none()
.inner_margin(8.0) .inner_margin(8.0)
.rounding(4.0) .rounding(4.0)
.fill(egui::Color32::WHITE) .fill(egui::Color32::WHITE)
.show(ui, |ui| { .show(ui, |ui| {
ui.label(egui::RichText::new(self.message).color(egui::Color32::BLACK)); ui.label(egui::RichText::new(&self.message).color(egui::Color32::BLACK));
}); });
}) })
.response .response

View File

@ -1,6 +1,6 @@
use eframe::egui::{self, Response, Ui, Widget}; use eframe::egui::{self, Response, Ui, Widget};
use super::state::AppState; use super::{model_response::ModelResponse, state::AppState};
pub struct ModelSelection<'a> { pub struct ModelSelection<'a> {
app_state: &'a mut AppState, app_state: &'a mut AppState,
@ -23,35 +23,46 @@ impl<'a> Widget for ModelSelection<'a> {
.selected_text("Models") .selected_text("Models")
.show_ui(ui, |ui| { .show_ui(ui, |ui| {
for model in self.models_available { for model in self.models_available {
let contained = self.app_state.model_selection.contains(&model); let selected_models = self
.app_state
.selected_models
.iter()
.map(|m| m.name.clone())
.collect::<Vec<String>>();
let contained = selected_models.contains(&model);
let label = ui.selectable_label(contained, &model); let label = ui.selectable_label(contained, &model);
if label.clicked() && !contained { if label.clicked() && !contained {
self.app_state.model_selection.push(model.clone()); self.app_state.selected_models.push(ModelResponse {
name: model.clone(),
message: "No message".to_string(),
status: 0,
time: 0,
});
} }
} }
}); });
let mut model_to_remove: Option<String> = None; let mut model_to_remove: Option<String> = None;
for model in &self.app_state.model_selection { for model in &self.app_state.selected_models {
egui::Frame::none() egui::Frame::none()
.inner_margin(8.0) .inner_margin(8.0)
.rounding(4.0) .rounding(4.0)
.fill(egui::Color32::WHITE) .fill(egui::Color32::WHITE)
.show(ui, |ui| { .show(ui, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label(model); ui.label(model.name.clone());
if ui.button("X").clicked() { if ui.button("X").clicked() {
model_to_remove.replace(model.clone()); model_to_remove.replace(model.name.clone());
} }
}); });
}); });
} }
if let Some(model) = model_to_remove { if let Some(model) = model_to_remove {
self.app_state.model_selection.retain(|m| m != &model); self.app_state.selected_models.retain(|m| m.name != model);
} }
}) })
.response .response

View File

@ -0,0 +1,29 @@
use eframe::egui::{Response, Ui, Widget};
use super::state::AppState;
pub struct PromptInput<'a> {
app_state: &'a mut AppState,
}
impl<'a> PromptInput<'a> {
pub fn new(app_state: &'a mut AppState) -> Self {
Self { app_state }
}
}
impl<'a> Widget for PromptInput<'a> {
fn ui(self, ui: &mut Ui) -> Response {
ui.horizontal(|ui| {
let label = ui.label("Prompt:");
ui.text_edit_singleline(&mut self.app_state.prompt_input)
.labelled_by(label.id);
if ui.button("Submit").clicked() {
self.app_state.prompt_input.clear();
}
});
ui.label(&self.app_state.prompt_input)
}
}

View File

@ -1,15 +1,17 @@
use eframe::CreationContext; use eframe::CreationContext;
use super::model_response::ModelResponse;
pub struct AppState { pub struct AppState {
pub counter: i32, pub selected_models: Vec<ModelResponse>,
pub model_selection: Vec<String>, pub prompt_input: String,
} }
impl AppState { impl AppState {
pub fn new(_cc: &CreationContext<'_>) -> Self { pub fn new(_cc: &CreationContext<'_>) -> Self {
Self { Self {
counter: 0, selected_models: vec![],
model_selection: vec![], prompt_input: String::new(),
} }
} }
} }