Gigishot commited on
Commit
a962133
·
verified ·
1 Parent(s): e70ecd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -6,7 +6,7 @@ import os
6
  # Load environment variables
7
  load_dotenv()
8
 
9
- # Hugging Face Inference Client
10
  client = InferenceClient(
11
  provider="hf-inference",
12
  api_key=os.getenv("HF_API_KEY")
@@ -14,29 +14,37 @@ client = InferenceClient(
14
 
15
  app = Flask(__name__)
16
 
 
17
  def generate_blog(paragraph_topic):
18
  try:
19
  response = client.chat.completions.create(
20
- model="HuggingFaceH4/zephyr-7b-alpha",
21
  messages=[
22
  {
23
  "role": "user",
24
- "content": f"Write a blog paragraph about: {paragraph_topic}"
25
  }
26
  ],
27
  max_tokens=300,
28
  )
29
  return response.choices[0].message.content
 
30
  except Exception as e:
31
  return f"Error generating blog: {str(e)}"
32
 
 
 
33
  @app.route("/", methods=["GET", "POST"])
34
  def index():
35
  paragraph = ""
36
  if request.method == "POST":
37
- topic = request.form["topic"]
38
- paragraph = generate_blog(topic)
 
 
39
  return render_template("index.html", paragraph=paragraph)
40
 
 
 
41
  if __name__ == "__main__":
42
  app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
 
6
  # Load environment variables
7
  load_dotenv()
8
 
9
+ # Initialize Hugging Face client
10
  client = InferenceClient(
11
  provider="hf-inference",
12
  api_key=os.getenv("HF_API_KEY")
 
14
 
15
  app = Flask(__name__)
16
 
17
+ # Blog generation function
18
  def generate_blog(paragraph_topic):
19
  try:
20
  response = client.chat.completions.create(
21
+ model="mistralai/Mistral-7B-Instruct-v0.2",
22
  messages=[
23
  {
24
  "role": "user",
25
+ "content": f"Write a well-structured blog paragraph about: {paragraph_topic}"
26
  }
27
  ],
28
  max_tokens=300,
29
  )
30
  return response.choices[0].message.content
31
+
32
  except Exception as e:
33
  return f"Error generating blog: {str(e)}"
34
 
35
+
36
+ # Route
37
  @app.route("/", methods=["GET", "POST"])
38
  def index():
39
  paragraph = ""
40
  if request.method == "POST":
41
+ topic = request.form.get("topic")
42
+ if topic:
43
+ paragraph = generate_blog(topic)
44
+
45
  return render_template("index.html", paragraph=paragraph)
46
 
47
+
48
+ # Run app
49
  if __name__ == "__main__":
50
  app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))