Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| from fastapi.responses import JSONResponse | |
| # Import semua fungsi dari sentiment_api | |
| from sentiment_api import analyze_sentiment, load_model, model_loaded | |
| # Create FastAPI app | |
| app = FastAPI( | |
| title="Indonesian Sentiment Analysis API", | |
| description="API untuk analisis sentimen bahasa Indonesia dengan dukungan bahasa gaul", | |
| version="1.0.0" | |
| ) | |
| # Add CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| class TextRequest(BaseModel): | |
| text: str | |
| # FastAPI endpoints | |
| async def root(): | |
| return { | |
| "message": "Indonesian Sentiment Analysis API", | |
| "version": "1.0.0", | |
| "docs": "/docs", | |
| "model_loaded": model_loaded, | |
| "status": "π Ready!" | |
| } | |
| async def health_check(): | |
| return { | |
| "status": "healthy", | |
| "model_loaded": model_loaded, | |
| "ready": True | |
| } | |
| async def predict_api(req: TextRequest): | |
| """API endpoint untuk analisis sentimen""" | |
| try: | |
| stars = analyze_sentiment(req.text) | |
| return JSONResponse(content={"stars": stars}) | |
| except Exception as e: | |
| return JSONResponse( | |
| status_code=500, | |
| content={"error": str(e), "stars": 3} | |
| ) | |
| # Gradio interface function | |
| def predict_gradio(text): | |
| """Function untuk Gradio interface""" | |
| if not text.strip(): | |
| return "β Mohon masukkan teks terlebih dahulu" | |
| try: | |
| stars = analyze_sentiment(text) | |
| if stars == 5: | |
| return f"βββββ ({stars}/5) - Sangat Positif!" | |
| elif stars == 4: | |
| return f"ββββ ({stars}/5) - Positif" | |
| elif stars == 3: | |
| return f"βββ ({stars}/5) - Netral" | |
| elif stars == 2: | |
| return f"ββ ({stars}/5) - Negatif" | |
| else: | |
| return f"β ({stars}/5) - Sangat Negatif" | |
| except Exception as e: | |
| return f"β Error: {str(e)}" | |
| # Create Gradio interface | |
| demo = gr.Interface( | |
| fn=predict_gradio, | |
| inputs=gr.Textbox( | |
| label="π Masukkan teks bahasa Indonesia", | |
| placeholder="Contoh: gue seneng banget hari ini", | |
| lines=3 | |
| ), | |
| outputs=gr.Textbox(label="π Hasil Analisis Sentimen"), | |
| title="π Indonesian Sentiment Analysis", | |
| description=""" | |
| **API analisis sentimen bahasa Indonesia dengan dukungan bahasa gaul menggunakan IndoBERT** | |
| π― **API Endpoints:** | |
| - `POST /predict` - Analisis sentimen (JSON: {"text": "your text"}) | |
| - `GET /` - Info API | |
| - `GET /health` - Health check | |
| - `GET /docs` - API documentation | |
| π **Response:** `{"stars": 1-5}` | |
| """, | |
| examples=[ | |
| ["gue seneng banget hari ini"], | |
| ["capek bgt tapi gak stress"], | |
| ["anjay mantul banget dah"], | |
| ["lagi gabut nih, bosen"], | |
| ["bahagia banget sama hasil ini"] | |
| ] | |
| ) | |
| # Mount Gradio on FastAPI | |
| app = gr.mount_gradio_app(app, demo, path="/") | |
| if __name__ == "__main__": | |
| demo.launch() |