File size: 5,398 Bytes
2436ec9 |
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
import gradio as gr
from models import LSP
from utils import logger, log_step
import asyncio
from typing import Optional
class LSPUI:
def __init__(self):
self.lsp: Optional[LSP] = None
self.setup_interface()
def validate_inputs(self, api_key: str, file: gr.File, instruction: str):
"""Validate input parameters"""
if not api_key:
return False, "Error: API key required"
if not file:
return False, "Error: No file uploaded"
if not instruction:
return False, "Error: No edit instruction"
return True, None
def read_file_content(self, file: gr.File) -> str:
"""Read content from file upload"""
if isinstance(file, str):
with open(file, 'r') as f:
return f.read()
return file.read().decode('utf-8')
async def process_smart(self, api_key: str, file: gr.File, instruction: str):
"""Handle smart approach independently"""
try:
# Validate and initialize
valid, error = self.validate_inputs(api_key, file, instruction)
if not valid:
return error, error, error
if not self.lsp:
self.lsp = LSP(api_key)
content = self.read_file_content(file)
log_step("UI", "Starting smart approach")
# Process
result, traces, elapsed_time = await self.lsp.edit_smart(content, instruction)
log_step("UI", f"Smart approach completed in {elapsed_time:.2f}s")
return (
result,
"\n".join(traces),
f"β Completed in {elapsed_time:.2f}s"
)
except Exception as e:
error_msg = f"Error: {str(e)}"
log_step("UI", f"Error in smart approach: {error_msg}")
return error_msg, error_msg, "β Failed"
async def process_naive(self, api_key: str, file: gr.File, instruction: str):
"""Handle naive approach independently"""
try:
# Validate and initialize
valid, error = self.validate_inputs(api_key, file, instruction)
if not valid:
return error, error, error
if not self.lsp:
self.lsp = LSP(api_key)
content = self.read_file_content(file)
log_step("UI", "Starting naive approach")
# Process
result, traces, elapsed_time = await self.lsp.edit_naive(content, instruction)
log_step("UI", f"Naive approach completed in {elapsed_time:.2f}s")
return (
result,
"\n".join(traces),
f"β Completed in {elapsed_time:.2f}s"
)
except Exception as e:
error_msg = f"Error: {str(e)}"
log_step("UI", f"Error in naive approach: {error_msg}")
return error_msg, error_msg, "β Failed"
def setup_interface(self):
"""Setup the Gradio interface"""
with gr.Blocks(title="LSP Comparison") as self.blocks:
gr.Markdown("# LLM Selective Processing Demo")
with gr.Row():
api_key = gr.Textbox(label="OpenAI API Key", type="password")
with gr.Row():
file_upload = gr.File(
label="Upload Markdown File",
file_types=[".md", ".txt"]
)
instruction = gr.Textbox(label="What to edit")
with gr.Row():
smart_btn = gr.Button("Update with LSP")
naive_btn = gr.Button("Update w/o LSP")
with gr.Row():
with gr.Column():
gr.Markdown("### Smart Approach (Section-Aware)")
smart_result = gr.Textbox(label="Result", lines=10, value="")
smart_trace = gr.Textbox(label="Traces", lines=10, value="")
smart_status = gr.Textbox(label="Status", value="")
with gr.Column():
gr.Markdown("### Naive Approach (Full Document)")
naive_result = gr.Textbox(label="Result", lines=10, value="")
naive_trace = gr.Textbox(label="Traces", lines=10, value="")
naive_status = gr.Textbox(label="Status", value="")
# Set up independent event handlers
smart_btn.click(
fn=lambda: ("Working on it...", "Processing...", "β³ Running..."),
outputs=[smart_result, smart_trace, smart_status]
).then(
fn=self.process_smart,
inputs=[api_key, file_upload, instruction],
outputs=[smart_result, smart_trace, smart_status]
)
naive_btn.click(
fn=lambda: ("Working on it...", "Processing...", "β³ Running..."),
outputs=[naive_result, naive_trace, naive_status]
).then(
fn=self.process_naive,
inputs=[api_key, file_upload, instruction],
outputs=[naive_result, naive_trace, naive_status]
)
def launch(self):
"""Launch the Gradio interface"""
self.blocks.launch()
|