Spaces:
Sleeping
Sleeping
Commit
Β·
b8eb8a3
1
Parent(s):
c0b77e8
batch analysis functions added
Browse files
app.py
CHANGED
@@ -49,6 +49,54 @@ def chinese_sentiment_analysis(text: str) -> str:
|
|
49 |
|
50 |
return json.dumps(result)
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
# gradio interface
|
53 |
demo = gr.TabbedInterface(
|
54 |
[
|
@@ -64,16 +112,35 @@ demo = gr.TabbedInterface(
|
|
64 |
fn = chinese_sentiment_analysis,
|
65 |
inputs = gr.Textbox(placeholder = 'θ¦εζηδΈζ...'),
|
66 |
outputs = gr.Textbox(),
|
67 |
-
title = '
|
68 |
description = 'Analyse the sentiment of Chinese text using SnowNLP',
|
69 |
-
api_name = 'chinese_sentiment_analysis'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
],
|
71 |
[
|
72 |
'sentiment analysis',
|
73 |
'chinese sentiment analysis',
|
|
|
|
|
74 |
]
|
75 |
)
|
76 |
|
77 |
# Launch the interface and MCP server
|
78 |
if __name__ == '__main__':
|
79 |
-
demo.launch(mcp_server = True)
|
|
|
49 |
|
50 |
return json.dumps(result)
|
51 |
|
52 |
+
|
53 |
+
def batch_sentiment_analysis(file_path: str) -> str:
|
54 |
+
'''
|
55 |
+
Batch process sentiment analysis from JSON file
|
56 |
+
|
57 |
+
Args:
|
58 |
+
file_path (str): Path to JSON file with {text: ''} format
|
59 |
+
|
60 |
+
Returns:
|
61 |
+
str: JSON string with analysis results in values
|
62 |
+
'''
|
63 |
+
with open(file_path, 'r', encoding = 'utf-8') as f:
|
64 |
+
data = json.load(f)
|
65 |
+
|
66 |
+
for key in data:
|
67 |
+
analysis_result = json.loads( sentiment_analysis(key) )
|
68 |
+
data[key] = analysis_result
|
69 |
+
|
70 |
+
output_path = f'processed_{file_path}'
|
71 |
+
with open(output_path, 'w', encoding = 'utf-8') as f:
|
72 |
+
json.dump(data, f, ensure_ascii = False, indent = 2)
|
73 |
+
|
74 |
+
return output_path
|
75 |
+
|
76 |
+
|
77 |
+
def batch_chinese_sentiment_analysis(file_path: str) -> str:
|
78 |
+
'''
|
79 |
+
Batch process Chinese sentiment analysis from JSON file
|
80 |
+
|
81 |
+
Args:
|
82 |
+
file_path (str): Path to JSON file with {text: ''} format
|
83 |
+
|
84 |
+
Returns:
|
85 |
+
str: JSON string with analysis results in values
|
86 |
+
'''
|
87 |
+
with open(file_path, 'r', encoding = 'utf-8') as f:
|
88 |
+
data = json.load(f)
|
89 |
+
|
90 |
+
for key in data:
|
91 |
+
analysis_result = json.loads( chinese_sentiment_analysis(key) )
|
92 |
+
data[key] = analysis_result
|
93 |
+
|
94 |
+
output_path = f'processed_{file_path}'
|
95 |
+
with open(output_path, 'w', encoding = 'utf-8') as f:
|
96 |
+
json.dump(data, f, ensure_ascii = False, indent = 2)
|
97 |
+
|
98 |
+
return output_path
|
99 |
+
|
100 |
# gradio interface
|
101 |
demo = gr.TabbedInterface(
|
102 |
[
|
|
|
112 |
fn = chinese_sentiment_analysis,
|
113 |
inputs = gr.Textbox(placeholder = 'θ¦εζηδΈζ...'),
|
114 |
outputs = gr.Textbox(),
|
115 |
+
title = 'δΈζζ
ζεζ',
|
116 |
description = 'Analyse the sentiment of Chinese text using SnowNLP',
|
117 |
+
api_name = 'chinese_sentiment_analysis'
|
118 |
+
),
|
119 |
+
gr.Interface(
|
120 |
+
fn = batch_sentiment_analysis,
|
121 |
+
inputs = gr.File(label = 'Upload JSON File'),
|
122 |
+
outputs = gr.File(label = 'Download Results'),
|
123 |
+
title = 'Batch Sentiment Analysis',
|
124 |
+
description = 'Process JSON file with multiple texts (English)',
|
125 |
+
api_name = 'batch_sentiment_analysis'
|
126 |
+
),
|
127 |
+
gr.Interface(
|
128 |
+
fn = batch_chinese_sentiment_analysis,
|
129 |
+
inputs = gr.File(label = 'δΈε³JSONζδ»Ά'),
|
130 |
+
outputs = gr.File(label = 'δΈθΌεζη΅ζ'),
|
131 |
+
title = 'ζΉιδΈζζ
ζεζ',
|
132 |
+
description = 'Batch process Chinese sentiment analysis from JSON file',
|
133 |
+
api_name = 'batch_chinese_sentiment_analysis'
|
134 |
+
)
|
135 |
],
|
136 |
[
|
137 |
'sentiment analysis',
|
138 |
'chinese sentiment analysis',
|
139 |
+
'batch processing',
|
140 |
+
'batch processing (Chinese text)'
|
141 |
]
|
142 |
)
|
143 |
|
144 |
# Launch the interface and MCP server
|
145 |
if __name__ == '__main__':
|
146 |
+
demo.launch(mcp_server = True)
|