Spaces:
Sleeping
Sleeping
import gradio as gr | |
from cohere.client import Client | |
from cohere.core import ApiError | |
import fitz | |
def extract_text_from_pdf(file_path): | |
doc = fitz.open(file_path) | |
text = "" | |
for page in doc: | |
text += page.get_text() | |
return text | |
def summarize_pdf(api_key, file): | |
try: | |
cli = Client(api_key) | |
text = extract_text_from_pdf(file.name) | |
response = cli.chat_stream( | |
message=f""" | |
I want a summary explaining the content of the following text, the summary must be in the same language the text is written in. | |
I want you to return directly the summary and ONLY the summary. The text is from a PDF file. | |
The text is as follows: | |
{text} | |
""", | |
model="command-nightly" | |
) | |
current_text = "" | |
for event in response: | |
if event.event_type == "text-generation": | |
current_text += event.text | |
yield current_text | |
except ApiError: | |
yield "La clé API est invalide." | |
# def summarize_text(api_key, text): | |
# try: | |
# cli = Client(api_key) | |
# response = cli.chat_stream( | |
# message=f""" | |
# I want a summary explaining the content ofthe following text, the summary must be in the same language the text is written in. | |
# I want you to return directly the summary and ONLY the summary. The text is as follows: | |
# {text} | |
# """, | |
# model="command-nightly" | |
# ) | |
# current_text = "" | |
# for event in response: | |
# if event.event_type == "text-generation": | |
# current_text += event.text | |
# yield current_text | |
# except ApiError as e: | |
# yield "La clé API est invalide." | |
# def summarize(api_key, file, text): | |
# if file is not None: | |
# yield from summarize_pdf(api_key, file) | |
# else: | |
# yield from summarize_text(api_key, text) | |
def summarize(api_key, file): | |
yield from summarize_pdf(api_key, file) | |
boxs = [ | |
gr.Textbox(lines=1, label="Cohere API Key", placeholder="Entrez votre clé API"), | |
gr.File(label="Le fichier à résumer"), | |
# gr.Textbox(lines=5, label="Le texte à résumer", placeholder="Entrez le texte à résumer") | |
] | |
output_box = gr.Textbox(label="le résumé") | |
gr.Interface( | |
fn=summarize, | |
inputs=boxs, | |
outputs=output_box, | |
description="Voici un outil pour résumer vos documents.", | |
title="Votre résumeur automatique de documents", | |
theme=gr.themes.Soft(), | |
allow_flagging=False | |
).launch() | |