Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
from textwrap import dedent
|
| 4 |
-
|
| 5 |
-
import streamlit as st
|
| 6 |
from crewai import Agent, Crew, Process, Task
|
| 7 |
from crewai_tools import SerperDevTool
|
| 8 |
from crewai import LLM
|
| 9 |
from typing import List
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# Configuration des clés API
|
| 12 |
os.environ["GEMINI_API_KEY"] = os.environ.get("GEMINI_API_KEY")
|
| 13 |
os.environ["SERPER_API_KEY"] = os.environ.get("SERPER_API_KEY")
|
| 14 |
|
| 15 |
llm = LLM(
|
| 16 |
model="gemini/gemini-1.5-flash",
|
| 17 |
-
temperature=
|
| 18 |
timeout=120,
|
| 19 |
max_tokens=8000,
|
| 20 |
)
|
|
@@ -49,7 +50,6 @@ writer = Agent(
|
|
| 49 |
def extract_json_from_result(result_text: str) -> list:
|
| 50 |
"""Extrait le JSON des résultats de la crew."""
|
| 51 |
try:
|
| 52 |
-
# Trouve le début et la fin du JSON dans le texte
|
| 53 |
json_start = result_text.find('[')
|
| 54 |
json_end = result_text.rfind(']') + 1
|
| 55 |
if json_start == -1 or json_end == 0:
|
|
@@ -60,7 +60,6 @@ def extract_json_from_result(result_text: str) -> list:
|
|
| 60 |
except (json.JSONDecodeError, ValueError) as e:
|
| 61 |
raise ValueError(f"Erreur lors de l'extraction du JSON : {str(e)}")
|
| 62 |
|
| 63 |
-
# Définition des tâches
|
| 64 |
def research_task(topic: str) -> Task:
|
| 65 |
return Task(
|
| 66 |
description=dedent(f"""Effectuer une recherche approfondie sur le sujet '{topic}'.
|
|
@@ -79,55 +78,42 @@ def flashcard_creation_task(research_task: Task) -> Task:
|
|
| 79 |
context=[research_task]
|
| 80 |
)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
topic = st.text_input("Entrez le sujet des flashcards:", "Intelligence Artificielle")
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
if not topic:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
for i, card in enumerate(flashcards, 1):
|
| 121 |
-
with st.expander(f"Flashcard {i}: {card['question']}", expanded=False):
|
| 122 |
-
st.write("**Réponse:**", card['answer'])
|
| 123 |
-
|
| 124 |
-
with tab2:
|
| 125 |
-
st.json(flashcards)
|
| 126 |
-
|
| 127 |
-
else:
|
| 128 |
-
st.error("Aucun résultat généré par la crew.")
|
| 129 |
-
|
| 130 |
-
except Exception as e:
|
| 131 |
-
st.error(f"Une erreur est survenue : {str(e)}")
|
| 132 |
-
st.write("Résultat brut pour débogage :")
|
| 133 |
-
st.write(result)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
from textwrap import dedent
|
|
|
|
|
|
|
| 5 |
from crewai import Agent, Crew, Process, Task
|
| 6 |
from crewai_tools import SerperDevTool
|
| 7 |
from crewai import LLM
|
| 8 |
from typing import List
|
| 9 |
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
# Configuration des clés API
|
| 13 |
os.environ["GEMINI_API_KEY"] = os.environ.get("GEMINI_API_KEY")
|
| 14 |
os.environ["SERPER_API_KEY"] = os.environ.get("SERPER_API_KEY")
|
| 15 |
|
| 16 |
llm = LLM(
|
| 17 |
model="gemini/gemini-1.5-flash",
|
| 18 |
+
temperature=0.7,
|
| 19 |
timeout=120,
|
| 20 |
max_tokens=8000,
|
| 21 |
)
|
|
|
|
| 50 |
def extract_json_from_result(result_text: str) -> list:
|
| 51 |
"""Extrait le JSON des résultats de la crew."""
|
| 52 |
try:
|
|
|
|
| 53 |
json_start = result_text.find('[')
|
| 54 |
json_end = result_text.rfind(']') + 1
|
| 55 |
if json_start == -1 or json_end == 0:
|
|
|
|
| 60 |
except (json.JSONDecodeError, ValueError) as e:
|
| 61 |
raise ValueError(f"Erreur lors de l'extraction du JSON : {str(e)}")
|
| 62 |
|
|
|
|
| 63 |
def research_task(topic: str) -> Task:
|
| 64 |
return Task(
|
| 65 |
description=dedent(f"""Effectuer une recherche approfondie sur le sujet '{topic}'.
|
|
|
|
| 78 |
context=[research_task]
|
| 79 |
)
|
| 80 |
|
| 81 |
+
@app.route('/')
|
| 82 |
+
def index():
|
| 83 |
+
return render_template('index.html')
|
|
|
|
| 84 |
|
| 85 |
+
@app.route('/generate', methods=['POST'])
|
| 86 |
+
def generate_flashcards():
|
| 87 |
+
topic = request.json.get('topic')
|
| 88 |
if not topic:
|
| 89 |
+
return jsonify({'error': 'Veuillez entrer un sujet.'}), 400
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
# Création des tâches
|
| 93 |
+
research = research_task(topic)
|
| 94 |
+
flashcard_creation = flashcard_creation_task(research)
|
| 95 |
+
|
| 96 |
+
# Création de la crew
|
| 97 |
+
crew = Crew(
|
| 98 |
+
agents=[researcher, writer],
|
| 99 |
+
tasks=[research, flashcard_creation],
|
| 100 |
+
process=Process.sequential,
|
| 101 |
+
verbose=True
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Exécution de la crew
|
| 105 |
+
result = crew.kickoff()
|
| 106 |
+
|
| 107 |
+
# Extraction du JSON depuis le dernier résultat de tâche
|
| 108 |
+
if result.tasks_output and len(result.tasks_output) > 0:
|
| 109 |
+
last_task_output = result.tasks_output[-1].raw
|
| 110 |
+
flashcards = extract_json_from_result(last_task_output)
|
| 111 |
+
return jsonify({'success': True, 'flashcards': flashcards})
|
| 112 |
+
else:
|
| 113 |
+
return jsonify({'error': 'Aucun résultat généré par la crew.'}), 500
|
| 114 |
+
|
| 115 |
+
except Exception as e:
|
| 116 |
+
return jsonify({'error': str(e)}), 500
|
| 117 |
+
|
| 118 |
+
if __name__ == '__main__':
|
| 119 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|