Gigishot commited on
Commit
ec7fa23
·
verified ·
1 Parent(s): d3c4072

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -37
app.py CHANGED
@@ -1,62 +1,53 @@
1
  from flask import Flask, render_template, request
2
  from dotenv import load_dotenv
3
- from huggingface_hub import InferenceClient
4
  import os
5
 
6
- # Load environment variables
7
  load_dotenv()
8
 
9
  app = Flask(__name__)
10
 
11
- # Initialize Hugging Face client
12
- client = InferenceClient(
13
- api_key=os.getenv("HF_API_KEY")
14
  )
15
 
16
- # Models to try
17
- MODELS = [
18
- "TinyLlama/TinyLlama-1.1B-Chat-v1.0",
19
- ]
20
-
21
  # Generate blog function
22
  def generate_blog(topic):
23
 
24
  prompt = f"""
25
- Write a short engaging blog about: {topic}
 
 
 
 
 
 
 
 
26
  """
27
 
28
  try:
29
- response = client.text_generation(
30
- model="google/gemma-2b-it",
31
- prompt=prompt,
32
- max_new_tokens=300,
 
 
 
 
 
33
  temperature=0.7,
 
34
  )
35
 
36
- return str(response)
37
 
38
  except Exception as e:
39
- return f"Error: {e}"
40
-
41
- for model in MODELS:
42
- try:
43
- response = client.text_generation(
44
- model=model,
45
- prompt=prompt,
46
- max_new_tokens=500,
47
- temperature=0.7,
48
- )
49
-
50
- return str(response).strip()
51
-
52
- except Exception as e:
53
- print(f"Error with model {model}: {e}")
54
- continue
55
-
56
- return "Error: All models failed."
57
 
58
 
59
- # Home Route
60
  @app.route("/", methods=["GET", "POST"])
61
  def index():
62
 
@@ -83,6 +74,5 @@ def index():
83
  )
84
 
85
 
86
- # Run app
87
  if __name__ == "__main__":
88
- app.run(debug=True)
 
1
  from flask import Flask, render_template, request
2
  from dotenv import load_dotenv
3
+ from groq import Groq
4
  import os
5
 
6
+ # Load env variables
7
  load_dotenv()
8
 
9
  app = Flask(__name__)
10
 
11
+ # Initialize Groq client
12
+ client = Groq(
13
+ api_key=os.getenv("GROQ_API_KEY")
14
  )
15
 
 
 
 
 
 
16
  # Generate blog function
17
  def generate_blog(topic):
18
 
19
  prompt = f"""
20
+ Write a detailed and engaging blog article on the topic: "{topic}"
21
+
22
+ The article should include:
23
+ - A catchy title
24
+ - Introduction
25
+ - Main content
26
+ - Conclusion
27
+
28
+ Keep it professional and readable.
29
  """
30
 
31
  try:
32
+
33
+ completion = client.chat.completions.create(
34
+ model="llama3-8b-8192",
35
+ messages=[
36
+ {
37
+ "role": "user",
38
+ "content": prompt
39
+ }
40
+ ],
41
  temperature=0.7,
42
+ max_tokens=700,
43
  )
44
 
45
+ return completion.choices[0].message.content
46
 
47
  except Exception as e:
48
+ return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
 
 
51
  @app.route("/", methods=["GET", "POST"])
52
  def index():
53
 
 
74
  )
75
 
76
 
 
77
  if __name__ == "__main__":
78
+ app.run(host="0.0.0.0", port=7860)