Spaces:
Sleeping
Sleeping
| """ | |
| Core - Settings Module | |
| Gestion de la clé API Gemini et génération de texte aléatoire. | |
| """ | |
| from google import genai | |
| # -- Stockage global de la clé API -- | |
| _api_key: str = "" | |
| _client = None | |
| def set_api_key(key: str) -> str: | |
| """ | |
| Sauvegarde la clé API Gemini et configure le client. | |
| Returns: | |
| Message de statut. | |
| """ | |
| global _api_key, _client | |
| _api_key = key.strip() | |
| if not _api_key: | |
| _client = None | |
| return "API key cleared." | |
| try: | |
| _client = genai.Client(api_key=_api_key) | |
| # Test rapide | |
| _client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents="Hi", | |
| ) | |
| return "Connected to Gemini API." | |
| except Exception as e: | |
| _client = None | |
| _api_key = "" | |
| return f"Error: {str(e)}" | |
| def get_api_key() -> str: | |
| """Retourne la clé API courante.""" | |
| return _api_key | |
| def is_configured() -> bool: | |
| """Vérifie si l'API Gemini est configurée.""" | |
| return _client is not None | |
| def generate_random_text(char_count: int = 200) -> str: | |
| """Génère une phrase aléatoire via Gemini API.""" | |
| if not is_configured(): | |
| return "Error: configure your Gemini API key in the Settings tab first." | |
| char_count = max(20, min(int(char_count), 2000)) | |
| prompt = ( | |
| f"Generate a single interesting sentence in English about a random topic " | |
| f"(science, technology, history, nature, etc.). " | |
| f"The sentence must be approximately {char_count} characters long. " | |
| f"Return ONLY the sentence, nothing else." | |
| ) | |
| try: | |
| response = _client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents=prompt, | |
| ) | |
| return response.text.strip() | |
| except Exception as e: | |
| return f"Error generating text: {str(e)}" | |
| def generate_categories() -> str: | |
| """Génère une liste de catégories aléatoires pour le zero-shot.""" | |
| if not is_configured(): | |
| return "Error: configure your Gemini API key in the Settings tab first." | |
| prompt = ( | |
| "Generate 5 random topic categories for text classification. " | |
| "Return ONLY the categories separated by commas, nothing else. " | |
| "Example: Politics, Technology, Sports, Science, Entertainment" | |
| ) | |
| try: | |
| response = _client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents=prompt, | |
| ) | |
| return response.text.strip() | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def generate_similar_pair(char_count: int = 100) -> tuple[str, str]: | |
| """Génère deux phrases ayant une similitude sémantique.""" | |
| if not is_configured(): | |
| err = "Error: configure your Gemini API key in the Settings tab first." | |
| return err, err | |
| char_count = max(20, min(int(char_count), 2000)) | |
| prompt = ( | |
| f"Generate two sentences in English that are related to the same topic " | |
| f"but worded differently. They should share some semantic similarity. " | |
| f"Each sentence must be approximately {char_count} characters long. " | |
| f"Return ONLY two lines, one sentence per line, nothing else." | |
| ) | |
| try: | |
| response = _client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents=prompt, | |
| ) | |
| lines = [l.strip() for l in response.text.strip().split("\n") if l.strip()] | |
| if len(lines) >= 2: | |
| return lines[0], lines[1] | |
| return lines[0] if lines else "Error: empty response", "" | |
| except Exception as e: | |
| err = f"Error: {str(e)}" | |
| return err, err | |
| def generate_ner_text(char_count: int = 200) -> str: | |
| """Génère un texte riche en entités nommées (personnes, lieux, organisations).""" | |
| if not is_configured(): | |
| return "Error: configure your Gemini API key in the Settings tab first." | |
| char_count = max(50, min(int(char_count), 2000)) | |
| prompt = ( | |
| f"Generate a news-style paragraph in English that mentions real people, " | |
| f"organizations, and locations by name (e.g., 'Elon Musk', 'NASA', 'Tokyo'). " | |
| f"The text must be approximately {char_count} characters long. " | |
| f"Return ONLY the paragraph, nothing else." | |
| ) | |
| try: | |
| response = _client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents=prompt, | |
| ) | |
| return response.text.strip() | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |