Spaces:
Sleeping
Sleeping
import gradio as gr | |
import anthropic | |
import time | |
from pypdf import PdfReader | |
import os | |
#print(os.getenv('api_key')) | |
api_key = os.getenv('api_key') | |
username = os.getenv('username') | |
password = os.getenv('password') | |
def getfromclaude(contente,prompte): | |
client = anthropic.Anthropic(api_key=api_key) | |
tt = time.time() | |
#https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching | |
response = client.beta.prompt_caching.messages.create( | |
model="claude-3-5-sonnet-20240620", | |
max_tokens=1024, | |
system=[ | |
{ | |
"type": "text", | |
"text": "Vous êtes un assistant IA chargé d'analyser des œuvres. Votre objectif est de fournir des commentaires perspicaces sur les thèmes et réponses.\n", | |
}, | |
{ | |
"type": "text", | |
"text": ""+contente, | |
"cache_control": {"type": "ephemeral"} | |
} | |
], | |
messages=[{"role": "user", "content": ""+prompte}], | |
) | |
#print(response) | |
print(response.usage) | |
#print("time : "+ str(time.time()-tt)) | |
return response.content[0].text | |
#def process2(prompte): | |
# return "fdsq" | |
def process2(ftext,prompte): | |
res= getfromclaude( ftext,prompte) | |
#print(res) | |
return res | |
# Fonction pour extraire le texte d'un PDF | |
def extract_text_from_pdf(pdf_file): | |
reader = PdfReader(pdf_file) | |
text = "" | |
for page in reader.pages: | |
text += page.extract_text() | |
return text | |
# Interface Gradio | |
def process_pdf_and_query(pdf_file, prompte): | |
text ="" | |
response = "" | |
if pdf_file is not None : | |
for i in pdf_file: | |
#print(i) | |
# Extraire le texte du PDF | |
text += extract_text_from_pdf(i) | |
response = process2(text,prompte) | |
return response | |
prompt = """ poses tes questions ici """ | |
# Création de l'interface Gradio | |
iface = gr.Interface( | |
fn=process_pdf_and_query, | |
inputs=[ | |
gr.File(file_count="multiple", label="Upload tes documents au format PDF"), | |
gr.Textbox(label="Poses tes questions", value=prompt) | |
], | |
outputs=gr.Textbox(label="Réponse"), | |
title="Assistant a l'analyse de documents", | |
description="Uploadez vos documents." | |
) | |
#iface.launch(share=True) | |
#iface.launch() | |
iface.launch(auth=(username, password)) | |