Mauro24 commited on
Commit
b1512b6
·
verified ·
1 Parent(s): 0a756b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -9
app.py CHANGED
@@ -1,13 +1,13 @@
1
  import openai
2
  import os
3
 
4
- # Recupera la chiave API dai secrets o da variabili d'ambiente
5
- api_key = os.getenv("OPENAI_API_KEY") # Assicurati che OPENAI_API_KEY sia nei Secrets
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
11
  openai.api_key = api_key
12
 
13
  def riassumi_testo(testo, max_token_riassunto):
@@ -34,12 +34,10 @@ def riassumi_testo(testo, max_token_riassunto):
34
  riassunto = risposta['choices'][0]['message']['content']
35
  return riassunto
36
 
37
- except openai.error.InvalidRequestError as e:
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,4 +57,3 @@ if __name__ == "__main__":
59
 
60
 
61
 
62
-
 
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):
 
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
 
58
 
59