| import streamlit as st |
| from google import genai |
| from google.genai.types import GenerateContentConfig, ThinkingConfig |
|
|
| def main(): |
| st.title("Interface Gemini API avec Thoughts") |
| |
| |
| api_key = st.text_input("Entrez votre clé API Google", type="password") |
| |
| |
| prompt = st.text_area("Entrez votre prompt") |
| |
| if st.button("Générer"): |
| if not api_key: |
| st.error("Veuillez entrer une clé API") |
| return |
| |
| if not prompt: |
| st.warning("Veuillez entrer un prompt") |
| return |
| |
| try: |
| |
| client = genai.Client( |
| api_key=api_key, |
| http_options={'api_version': 'v1alpha'} |
| ) |
| |
| |
| config = GenerateContentConfig( |
| thinking_config=ThinkingConfig(include_thoughts=True) |
| ) |
| |
| |
| with st.spinner("Génération en cours..."): |
| response = client.models.generate_content( |
| model="gemini-2.0-flash-thinking-exp-01-21", |
| contents=prompt, |
| config=config, |
| ) |
| |
| st.subheader("Résultats") |
| |
| print(response) |
| print(response.candidates[0].content.parts) |
| |
| for part in response.candidates[0].content.parts: |
| |
| if part.thought: |
| with st.expander("💭 Thoughts", expanded=True): |
| st.write(part.text) |
| else: |
| with st.expander("📝 Réponse", expanded=True): |
| st.write(part.text) |
| |
| except Exception as e: |
| st.error(f"Une erreur s'est produite: {str(e)}") |
|
|
| if __name__ == "__main__": |
| |
| st.set_page_config( |
| page_title="Interface Gemini API", |
| page_icon="🤖", |
| layout="wide" |
| ) |
| main() |
|
|