Update app.py
Browse files
app.py
CHANGED
@@ -460,6 +460,33 @@ async def edit_suffix(chunk_id, suffix):
|
|
460 |
result = api.editor.add_suffix(chunk_id, suffix)
|
461 |
return result, api.editor.diff(chunk_id, result)
|
462 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
463 |
def create_ui():
|
464 |
with gr.Blocks(title="Azure OpenAI Chat & Text Editor") as demo:
|
465 |
gr.Markdown("# Azure OpenAI Chat with Text Editing")
|
@@ -513,6 +540,20 @@ def create_ui():
|
|
513 |
prefix_btn.click(fn=edit_prefix, inputs=[chunk_id, prefix], outputs=[chunk_text, diff_output])
|
514 |
suffix_btn.click(fn=edit_suffix, inputs=[chunk_id, suffix], outputs=[chunk_text, diff_output])
|
515 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
516 |
with gr.Tab("Logs"):
|
517 |
logs = gr.Textbox(label="Application Logs", interactive=False)
|
518 |
logs_btn = gr.Button("Refresh Logs")
|
|
|
460 |
result = api.editor.add_suffix(chunk_id, suffix)
|
461 |
return result, api.editor.diff(chunk_id, result)
|
462 |
|
463 |
+
async def generate_and_edit(source_text, target_start, target_end, response_prompt):
|
464 |
+
# Step 1: Generate source paragraph/code
|
465 |
+
memory = ConversationMemory()
|
466 |
+
chunk_id = memory.add_chunk(source_text, "user")
|
467 |
+
logger.info(f"Generated source chunk: {chunk_id}")
|
468 |
+
|
469 |
+
# Step 2: Cut out the target text
|
470 |
+
api = OpenAIApi(api_key=os.getenv("AZURE_OPENAI_API_KEY"))
|
471 |
+
cut_result = api.editor.cut(chunk_id, target_start, target_end)
|
472 |
+
logger.info(f"Cut target text from chunk: {chunk_id}, start: {target_start}, end: {target_end}")
|
473 |
+
|
474 |
+
# Step 3: Generate response
|
475 |
+
response = await api.fetch_response(response_prompt)
|
476 |
+
if "error" not in response:
|
477 |
+
response_text = response["content"]
|
478 |
+
logger.info(f"Generated response: {response_text}")
|
479 |
+
else:
|
480 |
+
return "Error: Failed to generate response", ""
|
481 |
+
|
482 |
+
# Step 4: Paste response into the target hole
|
483 |
+
paste_result = api.editor.paste(chunk_id, target_start)
|
484 |
+
logger.info(f"Pasted response into chunk: {chunk_id}, position: {target_start}")
|
485 |
+
|
486 |
+
# Return updated text and diff
|
487 |
+
diff = api.editor.diff(chunk_id, paste_result) if "Error" not in paste_result else ""
|
488 |
+
return paste_result, diff
|
489 |
+
|
490 |
def create_ui():
|
491 |
with gr.Blocks(title="Azure OpenAI Chat & Text Editor") as demo:
|
492 |
gr.Markdown("# Azure OpenAI Chat with Text Editing")
|
|
|
540 |
prefix_btn.click(fn=edit_prefix, inputs=[chunk_id, prefix], outputs=[chunk_text, diff_output])
|
541 |
suffix_btn.click(fn=edit_suffix, inputs=[chunk_id, suffix], outputs=[chunk_text, diff_output])
|
542 |
|
543 |
+
with gr.Tab("Advanced Text Manipulation"):
|
544 |
+
source_text = gr.Textbox(label="Source Text", value="This is a sample paragraph. [TARGET] This is the rest of the text.")
|
545 |
+
target_start = gr.Number(label="Target Start Index", value=21, precision=0)
|
546 |
+
target_end = gr.Number(label="Target End Index", value=28, precision=0)
|
547 |
+
response_prompt = gr.Textbox(label="Response Prompt", value="Generate a response for the target section.")
|
548 |
+
generate_btn = gr.Button("Generate and Edit")
|
549 |
+
result_text = gr.Textbox(label="Result Text", interactive=False)
|
550 |
+
result_diff = gr.Textbox(label="Result Diff", interactive=False)
|
551 |
+
generate_btn.click(
|
552 |
+
fn=generate_and_edit,
|
553 |
+
inputs=[source_text, target_start, target_end, response_prompt],
|
554 |
+
outputs=[result_text, result_diff]
|
555 |
+
)
|
556 |
+
|
557 |
with gr.Tab("Logs"):
|
558 |
logs = gr.Textbox(label="Application Logs", interactive=False)
|
559 |
logs_btn = gr.Button("Refresh Logs")
|