Mauro24 commited on
Commit
09bc8bd
·
verified ·
1 Parent(s): b1512b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -1,14 +1,14 @@
1
  import openai
2
  import os
3
 
4
- # Configura la tua chiave API
5
- api_key = os.getenv("OPENAI_API_KEY") # Assicurati di impostare la chiave API come variabile d'ambiente
6
 
7
  if not api_key:
8
  raise ValueError("Chiave API OpenAI non trovata. Assicurati di aver impostato OPENAI_API_KEY.")
9
 
10
- # Configura l'API OpenAI
11
- openai.api_key = api_key
12
 
13
  def riassumi_testo(testo, max_token_riassunto):
14
  """
@@ -20,24 +20,26 @@ def riassumi_testo(testo, max_token_riassunto):
20
  """
21
  try:
22
  # Esegui la chiamata all'API di OpenAI
23
- risposta = openai.ChatCompletion.create(
24
- model="gpt-3.5-turbo",
25
  messages=[
26
  {"role": "system", "content": "Sei un assistente che riassume i testi in modo chiaro e conciso."},
27
  {"role": "user", "content": f"Riassumi il seguente testo: {testo}"}
28
  ],
29
- max_tokens=max_token_riassunto,
30
- temperature=0.5 # Controlla la creatività
31
  )
32
 
33
  # Estrai il riassunto dalla risposta
34
- riassunto = risposta['choices'][0]['message']['content']
35
  return riassunto
36
 
37
- except openai.error.OpenAIError as e: # Catch generico per OpenAI
38
- return f"Errore API di OpenAI: {e}"
 
 
39
  except Exception as e:
40
- return f"Errore generale: {str(e)}"
41
 
42
  # Test del codice
43
  if __name__ == "__main__":
@@ -57,3 +59,4 @@ if __name__ == "__main__":
57
 
58
 
59
 
 
 
1
  import openai
2
  import os
3
 
4
+ # Configura la tua chiave API in modo sicuro
5
+ api_key = os.getenv("OPENAI_API_KEY") # Imposta la chiave come variabile d'ambiente
6
 
7
  if not api_key:
8
  raise ValueError("Chiave API OpenAI non trovata. Assicurati di aver impostato OPENAI_API_KEY.")
9
 
10
+ # Crea il client utilizzando la chiave API
11
+ client = openai.Client(api_key=api_key)
12
 
13
  def riassumi_testo(testo, max_token_riassunto):
14
  """
 
20
  """
21
  try:
22
  # Esegui la chiamata all'API di OpenAI
23
+ risposta = client.chat.completions.create(
24
+ model="gpt-3.5-turbo", # Modello specifico
25
  messages=[
26
  {"role": "system", "content": "Sei un assistente che riassume i testi in modo chiaro e conciso."},
27
  {"role": "user", "content": f"Riassumi il seguente testo: {testo}"}
28
  ],
29
+ max_tokens=max_token_riassunto, # Limita i token
30
+ temperature=0.5, # Imposta la creatività
31
  )
32
 
33
  # Estrai il riassunto dalla risposta
34
+ riassunto = risposta.choices[0].message['content'] # Correzione nella lettura del riassunto
35
  return riassunto
36
 
37
+ except openai.error.BadRequestError as e: # Gestisci errore nella richiesta
38
+ return f"Errore nella richiesta: {e}"
39
+ except openai.error.AuthenticationError:
40
+ return "Errore di autenticazione: chiave API non valida."
41
  except Exception as e:
42
+ return f"Errore durante il riassunto: {str(e)}"
43
 
44
  # Test del codice
45
  if __name__ == "__main__":
 
59
 
60
 
61
 
62
+