Spaces:
Runtime error
Runtime error
NicolasGaudemet
commited on
Commit
•
5e73cde
1
Parent(s):
c7e22a1
Add application file
Browse files- summarizer_app.py +66 -0
summarizer_app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain import OpenAI, PromptTemplate, LLMChain
|
2 |
+
from langchain.text_splitter import CharacterTextSplitter
|
3 |
+
from langchain.chains.mapreduce import MapReduceChain
|
4 |
+
from langchain.prompts import PromptTemplate
|
5 |
+
from langchain.docstore.document import Document
|
6 |
+
from langchain.chains.summarize import load_summarize_chain
|
7 |
+
import json
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
# Configure votre clé API
|
11 |
+
openai.api_key = os.environ['OpenaiKey']
|
12 |
+
|
13 |
+
#définition du LLM
|
14 |
+
llm = OpenAI(temperature=0)
|
15 |
+
|
16 |
+
#chargement des paramètres
|
17 |
+
with open("parametres.json", "r") as p:
|
18 |
+
params = json.load(p)
|
19 |
+
taille_max = params["taille_max"]
|
20 |
+
chunks_max = taille_max//4000+1
|
21 |
+
|
22 |
+
#résumé d'un texte
|
23 |
+
|
24 |
+
def summarize_text(text_to_summarize, llm):
|
25 |
+
|
26 |
+
#préparation du texte
|
27 |
+
text_splitter = CharacterTextSplitter()
|
28 |
+
texts = text_splitter.split_text(text_to_summarize)
|
29 |
+
print(len(texts))
|
30 |
+
docs = [Document(page_content=t) for t in texts[:chunks_max]]
|
31 |
+
print(len(docs))
|
32 |
+
|
33 |
+
#résumé
|
34 |
+
chain = load_summarize_chain(llm, chain_type="map_reduce")
|
35 |
+
chain.run(docs)
|
36 |
+
|
37 |
+
chain = load_summarize_chain(llm, chain_type="map_reduce", return_intermediate_steps=True)
|
38 |
+
steps = chain({"input_documents": docs}, return_only_outputs=True)
|
39 |
+
|
40 |
+
print(len(steps['intermediate_steps']))
|
41 |
+
print(steps['intermediate_steps'])
|
42 |
+
|
43 |
+
return steps['output_text']
|
44 |
+
|
45 |
+
# Lecture et résumé d'un fichier texte
|
46 |
+
|
47 |
+
def summarize_uploaded_file(file):
|
48 |
+
if not file.name.endswith('.txt'):
|
49 |
+
return ("Le fichier doit être un fichier texte (.txt)")
|
50 |
+
with open(file.name, "r", encoding = "latin-1") as f:
|
51 |
+
text = f.read()
|
52 |
+
summary = summarize_text(text, llm)
|
53 |
+
return summary
|
54 |
+
|
55 |
+
# Création de l'interface Gradio
|
56 |
+
|
57 |
+
iface = gr.Interface(
|
58 |
+
fn=summarize_uploaded_file,
|
59 |
+
inputs="file",
|
60 |
+
outputs=gr.outputs.Textbox(label="Résumé"),
|
61 |
+
title="Text File Summarizer",
|
62 |
+
description=f"Résume un fichier texte de longueur jusqu'à {taille_max} tokens",
|
63 |
+
allow_flagging = "never")
|
64 |
+
|
65 |
+
# Lancer l'interface
|
66 |
+
iface.launch()
|