Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from google import genai
|
| 3 |
+
from google.genai.types import GenerateContentConfig, ThinkingConfig
|
| 4 |
+
|
| 5 |
+
def main():
|
| 6 |
+
st.title("Interface Gemini API avec Thoughts")
|
| 7 |
+
|
| 8 |
+
# Zone de saisie pour la clé API
|
| 9 |
+
api_key = st.text_input("Entrez votre clé API Google", type="password")
|
| 10 |
+
|
| 11 |
+
# Zone de texte pour le prompt
|
| 12 |
+
prompt = st.text_area("Entrez votre prompt")
|
| 13 |
+
|
| 14 |
+
if st.button("Générer"):
|
| 15 |
+
if not api_key:
|
| 16 |
+
st.error("Veuillez entrer une clé API")
|
| 17 |
+
return
|
| 18 |
+
|
| 19 |
+
if not prompt:
|
| 20 |
+
st.warning("Veuillez entrer un prompt")
|
| 21 |
+
return
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
# Configuration du client
|
| 25 |
+
client = genai.Client(
|
| 26 |
+
api_key=api_key,
|
| 27 |
+
http_options={'api_version':'v1alpha'}
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Configuration pour les thoughts
|
| 31 |
+
config = GenerateContentConfig(
|
| 32 |
+
thinking_config=ThinkingConfig(include_thoughts=True)
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Affichage d'un spinner pendant la génération
|
| 36 |
+
with st.spinner("Génération en cours..."):
|
| 37 |
+
response = client.models.generate_content(
|
| 38 |
+
model="gemini-2.0-flash-thinking-exp-01-21",
|
| 39 |
+
contents=prompt,
|
| 40 |
+
config=config
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Affichage des résultats dans des sections distinctes
|
| 44 |
+
st.subheader("Résultats")
|
| 45 |
+
|
| 46 |
+
for part in response.candidates[0].content.parts:
|
| 47 |
+
if part.thought:
|
| 48 |
+
with st.expander("💭 Thoughts", expanded=True):
|
| 49 |
+
st.write(part.text)
|
| 50 |
+
else:
|
| 51 |
+
with st.expander("📝 Réponse", expanded=True):
|
| 52 |
+
st.write(part.text)
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
st.error(f"Une erreur s'est produite: {str(e)}")
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
# Configuration de la page
|
| 59 |
+
st.set_page_config(
|
| 60 |
+
page_title="Interface Gemini API",
|
| 61 |
+
page_icon="🤖",
|
| 62 |
+
layout="wide"
|
| 63 |
+
)
|
| 64 |
+
main()
|