Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import io
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from pydub import AudioSegment
|
| 8 |
+
from huggingface_hub import InferenceApi
|
| 9 |
+
import json
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Générateur d'images
|
| 14 |
+
FLUX1_APIKEY = os.getenv('FLUX1_APIKEY')
|
| 15 |
+
# LLM
|
| 16 |
+
GEMINI_APIKEY = os.getenv('GEMINI_APIKEY')
|
| 17 |
+
# Modèle Whisper pour la transcription audio
|
| 18 |
+
WHISPER_MODEL = InferenceApi(repo_id="openai/whisper-large-v3")
|
| 19 |
+
|
| 20 |
+
def speech_to_text(audio):
|
| 21 |
+
audio_data = AudioSegment.from_file(audio)
|
| 22 |
+
audio_bytes = io.BytesIO()
|
| 23 |
+
audio_data.export(audio_bytes, format="wav")
|
| 24 |
+
response = WHISPER_MODEL({"inputs": audio_bytes.getvalue()})
|
| 25 |
+
return response['text']
|
| 26 |
+
|
| 27 |
+
def GenerateTextLLM(inputText):
|
| 28 |
+
url = f'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key={GEMINI_APIKEY}'
|
| 29 |
+
headers = {
|
| 30 |
+
'Content-Type': 'application/json'
|
| 31 |
+
}
|
| 32 |
+
data = {
|
| 33 |
+
"prompt": inputText,
|
| 34 |
+
"model": "gemini-1.5-flash-latest"
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
response = requests.post(url, headers=headers, json=data)
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
return json.loads(response.text)['candidates'][0]['content']['parts'][0]['text']
|
| 41 |
+
except:
|
| 42 |
+
return 'Error in LLM processing'
|
| 43 |
+
|
| 44 |
+
def GenerateImageFromText(prompt):
|
| 45 |
+
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
| 46 |
+
headers = {"Authorization": f"Bearer {FLUX1_APIKEY}"}
|
| 47 |
+
|
| 48 |
+
def query(payload):
|
| 49 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 50 |
+
return response.content
|
| 51 |
+
|
| 52 |
+
image_bytes = query({"inputs": prompt})
|
| 53 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 54 |
+
|
| 55 |
+
return image
|
| 56 |
+
|
| 57 |
+
# Fonction principale qui traite soit le texte soit l'audio
|
| 58 |
+
def Main(text_input, audio_input):
|
| 59 |
+
if text_input:
|
| 60 |
+
input_data = text_input
|
| 61 |
+
elif audio_input:
|
| 62 |
+
input_data = speech_to_text(audio_input)
|
| 63 |
+
else:
|
| 64 |
+
return "Veuillez fournir un texte ou un fichier audio", None
|
| 65 |
+
|
| 66 |
+
# Générer le résumé du LLM en plusieurs axes
|
| 67 |
+
summarized_text = GenerateTextLLM(input_data)
|
| 68 |
+
axes = summarized_text.split("\n") # Supposons que le LLM renvoie un texte structuré
|
| 69 |
+
|
| 70 |
+
# Générer une image pour chaque axe
|
| 71 |
+
images = [GenerateImageFromText(axis) for axis in axes if axis]
|
| 72 |
+
|
| 73 |
+
return images, summarized_text
|
| 74 |
+
|
| 75 |
+
# Interface Gradio
|
| 76 |
+
inputs = [
|
| 77 |
+
gr.inputs.Textbox(label="Texte (laisser vide si audio fourni)", lines=5, placeholder="Entrez votre texte ici..."),
|
| 78 |
+
gr.inputs.Audio(source="upload", type="file", label="Fichier audio (laisser vide si texte fourni)")
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
outputs = [
|
| 82 |
+
gr.outputs.Gallery(label="Diapositives générées"),
|
| 83 |
+
gr.outputs.Textbox(label="Résumé en axes")
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
interface = gr.Interface(
|
| 87 |
+
fn=Main,
|
| 88 |
+
inputs=inputs,
|
| 89 |
+
outputs=outputs,
|
| 90 |
+
title="Résumé et Génération de Diapositives",
|
| 91 |
+
description="Entrez un texte ou un fichier audio, le modèle LLM résumera l'entrée en plusieurs axes avec des mots-clés, et des diapositives seront générées pour chaque axe."
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
interface.launch()
|