bencser commited on
Commit
7dabf28
·
verified ·
1 Parent(s): ece12d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -20
app.py CHANGED
@@ -1,12 +1,16 @@
1
  import os
 
2
  from groq import Groq
3
- from flask import Flask, render_template
4
  from dotenv import load_dotenv
5
 
6
  load_dotenv()
7
 
8
  app = Flask(__name__)
9
 
 
 
 
10
  # Set Groq API key
11
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
12
 
@@ -14,31 +18,39 @@ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
14
  model = "llama3-groq-70b-8192-tool-use-preview"
15
 
16
  # Set fixed prompt
17
- fixed_prompt = "generate a concise silly letter to parents about their child detention reasons. write extreme behaviours"
18
 
19
  # Define function to generate text
20
  def generate_text():
21
- completion = client.chat.completions.create(
22
- model=model,
23
- messages=[
24
- {
25
- "role": "user",
26
- "content": fixed_prompt
27
- }
28
- ],
29
- temperature=0.5,
30
- max_tokens=1024,
31
- top_p=0.65,
32
- stream=False,
33
- stop=None,
34
- )
35
- return completion.choices[0].message.content.strip()
 
 
 
 
36
 
37
  # Define Flask route
38
  @app.route("/")
39
  def home():
40
- message = generate_text()
41
- return render_template("index.html", message=message)
 
 
 
 
42
 
43
  if __name__ == "__main__":
44
- app.run(host="0.0.0.0", port=7860, debug=False)
 
1
  import os
2
+ import logging
3
  from groq import Groq
4
+ from flask import Flask
5
  from dotenv import load_dotenv
6
 
7
  load_dotenv()
8
 
9
  app = Flask(__name__)
10
 
11
+ # Set up logging
12
+ logging.basicConfig(level=logging.DEBUG)
13
+
14
  # Set Groq API key
15
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
16
 
 
18
  model = "llama3-groq-70b-8192-tool-use-preview"
19
 
20
  # Set fixed prompt
21
+ fixed_prompt = "generate a silly letter to parents about their child detention reasons. write extreme behaviours"
22
 
23
  # Define function to generate text
24
  def generate_text():
25
+ try:
26
+ completion = client.chat.completions.create(
27
+ model=model,
28
+ messages=[
29
+ {
30
+ "role": "user",
31
+ "content": fixed_prompt
32
+ }
33
+ ],
34
+ temperature=0.5,
35
+ max_tokens=1024,
36
+ top_p=0.65,
37
+ stream=False,
38
+ stop=None,
39
+ )
40
+ return completion.choices[0].message.content.strip()
41
+ except Exception as e:
42
+ app.logger.error(f"Error generating text: {str(e)}")
43
+ return "An error occurred while generating the text."
44
 
45
  # Define Flask route
46
  @app.route("/")
47
  def home():
48
+ try:
49
+ message = generate_text()
50
+ return f"<pre>{message}</pre>"
51
+ except Exception as e:
52
+ app.logger.error(f"Error in home route: {str(e)}")
53
+ return "An error occurred. Please try again later.", 500
54
 
55
  if __name__ == "__main__":
56
+ app.run(host="0.0.0.0", port=7860)