chetanganatra commited on
Commit
98fba7d
·
verified ·
1 Parent(s): e7a3e9b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -0
app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import re
4
+ from typing import List, Dict
5
+
6
+ def split_text_into_sentences(text: str) -> Dict:
7
+ """
8
+ Split text into sentences and return as JSON
9
+
10
+ Args:
11
+ text (str): Input text paragraph
12
+
13
+ Returns:
14
+ Dict: JSON response with sentences and metadata
15
+ """
16
+ if not text or not text.strip():
17
+ return {
18
+ "status": "error",
19
+ "message": "Empty input text",
20
+ "sentences": [],
21
+ "count": 0
22
+ }
23
+
24
+ # Clean the text
25
+ text = text.strip()
26
+
27
+ # Simple sentence splitting using regex
28
+ # This pattern looks for sentence endings followed by whitespace or end of string
29
+ sentence_pattern = r'(?<=[.!?])\s+(?=[A-Z])'
30
+
31
+ # Split the text
32
+ sentences = re.split(sentence_pattern, text)
33
+
34
+ # Clean up sentences (remove extra whitespace)
35
+ sentences = [sentence.strip() for sentence in sentences if sentence.strip()]
36
+
37
+ # Create response
38
+ response = {
39
+ "status": "success",
40
+ "sentences": sentences,
41
+ "count": len(sentences),
42
+ "original_length": len(text),
43
+ "metadata": {
44
+ "avg_sentence_length": sum(len(s) for s in sentences) / len(sentences) if sentences else 0,
45
+ "longest_sentence": max(len(s) for s in sentences) if sentences else 0,
46
+ "shortest_sentence": min(len(s) for s in sentences) if sentences else 0
47
+ }
48
+ }
49
+
50
+ return response
51
+
52
+ def format_json_output(result: Dict) -> str:
53
+ """Format the result as pretty JSON string"""
54
+ return json.dumps(result, indent=2, ensure_ascii=False)
55
+
56
+ # Create Gradio interface
57
+ with gr.Blocks(title="Text to Sentences API") as demo:
58
+ gr.Markdown("# Text to Sentences Splitter API")
59
+ gr.Markdown("Enter a text paragraph and get it split into sentences with JSON output.")
60
+
61
+ with gr.Row():
62
+ with gr.Column():
63
+ input_text = gr.Textbox(
64
+ label="Input Text",
65
+ placeholder="Enter your text paragraph here...",
66
+ lines=5,
67
+ max_lines=10
68
+ )
69
+ submit_btn = gr.Button("Split into Sentences", variant="primary")
70
+
71
+ with gr.Column():
72
+ output_json = gr.JSON(
73
+ label="JSON Output",
74
+ show_label=True
75
+ )
76
+
77
+ # Example inputs
78
+ gr.Examples(
79
+ examples=[
80
+ ["Hello world! How are you today? I hope you're doing well. This is a test sentence."],
81
+ ["The quick brown fox jumps over the lazy dog. Machine learning is fascinating! Natural language processing involves many complex tasks. Text processing is an important skill."],
82
+ ["What is artificial intelligence? AI refers to computer systems that can perform tasks typically requiring human intelligence. These systems can learn, reason, and adapt to new situations."]
83
+ ],
84
+ inputs=input_text,
85
+ outputs=output_json,
86
+ fn=split_text_into_sentences,
87
+ cache_examples=True
88
+ )
89
+
90
+ # Connect the interface
91
+ submit_btn.click(
92
+ fn=split_text_into_sentences,
93
+ inputs=input_text,
94
+ outputs=output_json
95
+ )
96
+
97
+ # API documentation
98
+ gr.Markdown("""
99
+ ## API Usage
100
+
101
+ This app provides both a web interface and API endpoints.
102
+
103
+ ### Using the API programmatically:
104
+
105
+ ```python
106
+ import requests
107
+ import json
108
+
109
+ # Replace with your actual Hugging Face Space URL
110
+ url = "https://your-username-text-splitter.hf.space/api/predict"
111
+
112
+ payload = {
113
+ "data": ["Your text paragraph here..."]
114
+ }
115
+
116
+ response = requests.post(url, json=payload)
117
+ result = response.json()
118
+ print(json.dumps(result["data"][0], indent=2))
119
+ ```
120
+
121
+ ### cURL example:
122
+ ```bash
123
+ curl -X POST https://your-username-text-splitter.hf.space/api/predict \
124
+ -H "Content-Type: application/json" \
125
+ -d '{"data": ["Hello world! How are you? This is a test."]}'
126
+ ```
127
+
128
+ ### Response format:
129
+ ```json
130
+ {
131
+ "status": "success",
132
+ "sentences": ["Hello world!", "How are you?", "This is a test."],
133
+ "count": 3,
134
+ "original_length": 45,
135
+ "metadata": {
136
+ "avg_sentence_length": 15.0,
137
+ "longest_sentence": 17,
138
+ "shortest_sentence": 12
139
+ }
140
+ }
141
+ ```
142
+ """)
143
+
144
+ # Launch the app
145
+ if __name__ == "__main__":
146
+ demo.launch()