Spaces:
Sleeping
Sleeping
ilanaliouchouche
commited on
Commit
·
206d14a
1
Parent(s):
7e10837
1st integration with cohere API
Browse files- app.py +84 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from cohere.client import Client
|
3 |
+
from cohere.core import ApiError
|
4 |
+
import fitz
|
5 |
+
|
6 |
+
def extract_text_from_pdf(file_path):
|
7 |
+
doc = fitz.open(file_path)
|
8 |
+
text = ""
|
9 |
+
for page in doc:
|
10 |
+
text += page.get_text()
|
11 |
+
return text
|
12 |
+
|
13 |
+
def summarize_pdf(api_key, file):
|
14 |
+
try:
|
15 |
+
cli = Client(api_key)
|
16 |
+
|
17 |
+
text = extract_text_from_pdf(file.name)
|
18 |
+
|
19 |
+
response = cli.chat_stream(
|
20 |
+
message=f"""
|
21 |
+
I want a summary explaining the content of the following text, the summary must be in the same language the text is written in.
|
22 |
+
I want you to return directly the summary and ONLY the summary. The text is from a PDF file.
|
23 |
+
The text is as follows:
|
24 |
+
{text}
|
25 |
+
""",
|
26 |
+
model="command-nightly"
|
27 |
+
)
|
28 |
+
|
29 |
+
current_text = ""
|
30 |
+
for event in response:
|
31 |
+
if event.event_type == "text-generation":
|
32 |
+
current_text += event.text
|
33 |
+
yield current_text
|
34 |
+
|
35 |
+
except ApiError:
|
36 |
+
yield "La clé API est invalide."
|
37 |
+
|
38 |
+
# def summarize_text(api_key, text):
|
39 |
+
# try:
|
40 |
+
# cli = Client(api_key)
|
41 |
+
# response = cli.chat_stream(
|
42 |
+
# message=f"""
|
43 |
+
# I want a summary explaining the content ofthe following text, the summary must be in the same language the text is written in.
|
44 |
+
# I want you to return directly the summary and ONLY the summary. The text is as follows:
|
45 |
+
# {text}
|
46 |
+
# """,
|
47 |
+
# model="command-nightly"
|
48 |
+
# )
|
49 |
+
|
50 |
+
# current_text = ""
|
51 |
+
# for event in response:
|
52 |
+
# if event.event_type == "text-generation":
|
53 |
+
# current_text += event.text
|
54 |
+
# yield current_text
|
55 |
+
|
56 |
+
# except ApiError as e:
|
57 |
+
# yield "La clé API est invalide."
|
58 |
+
|
59 |
+
# def summarize(api_key, file, text):
|
60 |
+
# if file is not None:
|
61 |
+
# yield from summarize_pdf(api_key, file)
|
62 |
+
# else:
|
63 |
+
# yield from summarize_text(api_key, text)
|
64 |
+
|
65 |
+
def summarize(api_key, file):
|
66 |
+
yield from summarize_pdf(api_key, file)
|
67 |
+
|
68 |
+
boxs = [
|
69 |
+
gr.Textbox(lines=1, label="Cohere API Key", placeholder="Entrez votre clé API"),
|
70 |
+
gr.File(label="Le fichier à résumer"),
|
71 |
+
# gr.Textbox(lines=5, label="Le texte à résumer", placeholder="Entrez le texte à résumer")
|
72 |
+
]
|
73 |
+
|
74 |
+
output_box = gr.Textbox(label="le résumé")
|
75 |
+
|
76 |
+
gr.Interface(
|
77 |
+
fn=summarize,
|
78 |
+
inputs=boxs,
|
79 |
+
outputs=output_box,
|
80 |
+
description="Voici un outil pour résumer vos documents.",
|
81 |
+
title="Votre résumeur automatique de documents",
|
82 |
+
theme=gr.themes.Soft(),
|
83 |
+
allow_flagging=False
|
84 |
+
).launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
cohere==5.5.6
|
2 |
+
PyMuPDF==1.24.9
|