Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import textblob, json | |
| import os | |
| from supabase import create_client, Client | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # Meilleure pratique : utiliser les variables d'environnement quand possible | |
| url: str = os.getenv("NEXT_PUBLIC_SUPABASE_URL") | |
| key: str = os.getenv("NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY") | |
| # Création du client | |
| supabase: Client = create_client(url, key) | |
| print("✅ Connexion à Supabase réussie !") | |
| def get_from_db(query:str)->str: | |
| """ | |
| This function takes a sql query as input and returns result after excecute it in the database. | |
| Args: | |
| query (str): SQL Query. | |
| Returns: | |
| str: A JSON string containing the result | |
| """ | |
| # === Configuration === | |
| response = supabase.rpc( | |
| "run_sql", | |
| {"query": query} | |
| ).execute() | |
| return json.dumps(response.data, indent=2) | |
| def sentiment_analysis(text:str)-> str: | |
| """ | |
| This function takes a string input and returns the sentiment polarity of the text. | |
| Args: | |
| text (str): The input text for which the sentiment analysis is to be performed. | |
| Returns: | |
| str: A JSON string containing polarity, subjectivity, and assessment | |
| """ | |
| out = textblob.TextBlob(text) | |
| result = out.sentiment | |
| result = { | |
| "polarity": result.polarity, | |
| "subjectivity": result.subjectivity, | |
| "assessment": "positive" if result.polarity > 0 else "negative" if result.polarity < 0 else "neutral" | |
| } | |
| return json.dumps(result, indent=4) | |
| sentiment_interface = gr.Interface( | |
| fn=sentiment_analysis, | |
| inputs=gr.Textbox(placeholder="Enter the text to analyse"), | |
| outputs=gr.Textbox(), | |
| title="Sentiment Analysis", | |
| description="Analyse le sentiment d'un texte (positive / negative / neutral)." | |
| ) | |
| sql_excec_interface = gr.Interface( | |
| fn=get_from_db, | |
| inputs=gr.Textbox(placeholder="Enter Sql Query"), | |
| outputs=gr.Textbox(), | |
| title="Sql Runner in DB", | |
| description="Return the result from DB." | |
| ) | |
| # ==================== Combinaison en onglets (TabbedInterface) ==================== | |
| demo = gr.TabbedInterface( | |
| [sentiment_interface, sql_excec_interface], | |
| ["Sentiment Analysis", "SQL RUNNER"], | |
| title="MCP Agent - Multi Tools" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(mcp_server=True, share=True) |