Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,16 @@
|
|
1 |
import os
|
|
|
2 |
from groq import Groq
|
3 |
-
from flask import Flask
|
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
|
18 |
|
19 |
# Define function to generate text
|
20 |
def generate_text():
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Define Flask route
|
38 |
@app.route("/")
|
39 |
def home():
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
|
43 |
if __name__ == "__main__":
|
44 |
-
app.run(host="0.0.0.0", port=7860
|
|
|
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)
|