Spaces:
Sleeping
Sleeping
File size: 4,975 Bytes
42d64b4 |
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 140 141 142 143 144 145 |
#!/usr/bin/env python3
"""
Hugging Face Space app for CoEdIT Handler
"""
import gradio as gr
import sys
import os
import json
# Add current directory to path so we can import handler
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from handler import EndpointHandler
# Initialize the handler
print("🚀 Initializing CoEdIT Handler...")
try:
handler = EndpointHandler("grammarly/coedit-large")
print("✅ Handler initialized successfully")
except Exception as e:
print(f"❌ Failed to initialize handler: {e}")
handler = None
def process_text(text, num_return_sequences=1, temperature=1.0):
"""Process text through the CoEdIT handler"""
if handler is None:
return "❌ Handler not initialized. Please check the logs."
try:
# Prepare input for the handler
inputs = {
"inputs": [text],
"parameters": {
"num_return_sequences": num_return_sequences,
"temperature": temperature
}
}
# Process through handler
result = handler(inputs)
if result.get("success", False):
results = result.get("results", [])
if results:
enhanced = results[0].get("enhanced_sentence", "")
changes = results[0].get("changes", [])
# Format the response
response = f"**Enhanced Text:**\n{enhanced}\n\n"
if changes:
response += "**Changes Made:**\n"
for i, change in enumerate(changes, 1):
original = change.get("original_phrase", "")
new = change.get("new_phrase", "")
if original and new:
response += f"{i}. '{original}' → '{new}'\n"
return response
else:
return "No results returned."
else:
return f"❌ Error: {result.get('error', 'Unknown error')}"
except Exception as e:
return f"❌ Error processing text: {str(e)}"
# Create Gradio interface
def create_interface():
with gr.Blocks(title="CoEdIT Handler", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# CoEdIT Text Editor
This is a custom handler for the Grammarly CoEdIT model, providing grammar correction and text enhancement.
""")
with gr.Row():
with gr.Column():
input_text = gr.Textbox(
label="Input Text",
placeholder="Fix the grammar: When I grow up, I start to understand what he said is quite right.",
lines=3
)
with gr.Row():
num_sequences = gr.Slider(
minimum=1,
maximum=5,
value=1,
step=1,
label="Number of variations"
)
temperature = gr.Slider(
minimum=0.1,
maximum=2.0,
value=1.0,
step=0.1,
label="Temperature"
)
process_btn = gr.Button("Process Text", variant="primary")
with gr.Column():
output_text = gr.Markdown(label="Enhanced Text")
# Example texts
gr.Examples(
examples=[
"Fix the grammar: When I grow up, I start to understand what he said is quite right.",
"Make this text coherent: Their flight is weak. They run quickly through the tree canopy.",
"Rewrite to make this easier to understand: A storm surge is what forecasters consider a hurricane's most treacherous aspect.",
"Paraphrase this: Do you know where I was born?",
"Write this more formally: omg i love that song im listening to it right now"
],
inputs=input_text
)
# Event handlers
process_btn.click(
fn=process_text,
inputs=[input_text, num_sequences, temperature],
outputs=output_text
)
# API endpoint info
gr.Markdown("""
## API Endpoint
This Space also provides an API endpoint at `/predict` for programmatic access:
```bash
curl -X POST "https://your-space-url.hf.space/predict" \\
-H "Content-Type: application/json" \\
-d '{"inputs": ["Your text here"]}'
```
""")
return demo
# Create the interface
if __name__ == "__main__":
demo = create_interface()
demo.launch(server_name="0.0.0.0", server_port=7860)
|