initial commit
Browse files- app.py +114 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import logging
|
3 |
+
import sys
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Configure logging
|
7 |
+
logging.basicConfig(
|
8 |
+
level=logging.INFO,
|
9 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
10 |
+
handlers=[logging.StreamHandler(sys.stdout)]
|
11 |
+
)
|
12 |
+
logger = logging.getLogger(__name__)
|
13 |
+
|
14 |
+
# Log startup information
|
15 |
+
logger.info("Starting StudAI Summarization Service with Gradio")
|
16 |
+
logger.info(f"Python version: {sys.version}")
|
17 |
+
|
18 |
+
# Import model
|
19 |
+
try:
|
20 |
+
from transformers import pipeline
|
21 |
+
logger.info("Loading summarization model (t5-small)...")
|
22 |
+
|
23 |
+
summarizer = pipeline(
|
24 |
+
"summarization",
|
25 |
+
model="t5-small",
|
26 |
+
device=-1 # Use CPU for more reliable execution
|
27 |
+
)
|
28 |
+
logger.info("Model loaded successfully!")
|
29 |
+
model_available = True
|
30 |
+
except Exception as e:
|
31 |
+
logger.error(f"Failed to load model: {str(e)}")
|
32 |
+
model_available = False
|
33 |
+
|
34 |
+
def summarize_text(text, max_length=150, min_length=30):
|
35 |
+
"""Summarize the provided text using the loaded model"""
|
36 |
+
try:
|
37 |
+
if not text or len(text) < 50:
|
38 |
+
return text
|
39 |
+
|
40 |
+
if not model_available:
|
41 |
+
return "Error: Summarization model is not available"
|
42 |
+
|
43 |
+
logger.info(f"Summarizing text of length {len(text)}")
|
44 |
+
result = summarizer(
|
45 |
+
text,
|
46 |
+
max_length=max_length,
|
47 |
+
min_length=min_length,
|
48 |
+
truncation=True
|
49 |
+
)
|
50 |
+
summary = result[0]["summary_text"]
|
51 |
+
logger.info(f"Generated summary of length {len(summary)}")
|
52 |
+
return summary
|
53 |
+
except Exception as e:
|
54 |
+
logger.error(f"Error during summarization: {str(e)}")
|
55 |
+
return f"Error: {str(e)}"
|
56 |
+
|
57 |
+
def api_summarize(text, max_length=150, min_length=30):
|
58 |
+
"""API function for summarization"""
|
59 |
+
summary = summarize_text(text, max_length, min_length)
|
60 |
+
return {"summary": summary}
|
61 |
+
|
62 |
+
# Create Gradio interface
|
63 |
+
with gr.Blocks(title="StudAI Summarization") as demo:
|
64 |
+
gr.Markdown("# StudAI Text Summarization")
|
65 |
+
gr.Markdown("This service provides text summarization for the StudAI Android app.")
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
with gr.Column():
|
69 |
+
input_text = gr.Textbox(
|
70 |
+
label="Input Text",
|
71 |
+
placeholder="Enter text to summarize (at least 50 characters)",
|
72 |
+
lines=10
|
73 |
+
)
|
74 |
+
with gr.Row():
|
75 |
+
max_length = gr.Slider(
|
76 |
+
label="Max Length",
|
77 |
+
minimum=50,
|
78 |
+
maximum=500,
|
79 |
+
value=150,
|
80 |
+
step=10
|
81 |
+
)
|
82 |
+
min_length = gr.Slider(
|
83 |
+
label="Min Length",
|
84 |
+
minimum=10,
|
85 |
+
maximum=200,
|
86 |
+
value=30,
|
87 |
+
step=5
|
88 |
+
)
|
89 |
+
submit_btn = gr.Button("Summarize")
|
90 |
+
|
91 |
+
with gr.Column():
|
92 |
+
output_text = gr.Textbox(label="Summary", lines=10)
|
93 |
+
|
94 |
+
submit_btn.click(
|
95 |
+
fn=summarize_text,
|
96 |
+
inputs=[input_text, max_length, min_length],
|
97 |
+
outputs=output_text
|
98 |
+
)
|
99 |
+
|
100 |
+
# Add API endpoints for Android app
|
101 |
+
gr.Interface(
|
102 |
+
fn=api_summarize,
|
103 |
+
inputs=[
|
104 |
+
gr.Textbox(label="text"),
|
105 |
+
gr.Number(label="max_length", default=150),
|
106 |
+
gr.Number(label="min_length", default=30)
|
107 |
+
],
|
108 |
+
outputs=gr.JSON(),
|
109 |
+
title="Summarization API",
|
110 |
+
description="API for StudAI Android app"
|
111 |
+
).launch(show_api=True)
|
112 |
+
|
113 |
+
# Launch the app
|
114 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==4.13.0
|
2 |
+
transformers==4.35.2
|
3 |
+
torch==2.0.1
|
4 |
+
numpy<2.0.0
|
5 |
+
pydantic==2.4.2
|
6 |
+
requests==2.31.0
|