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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -3,10 +3,10 @@ from dotenv import load_dotenv
3
  from huggingface_hub import InferenceClient
4
  import os
5
 
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,29 +14,27 @@ client = InferenceClient(
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:
@@ -45,6 +43,5 @@ def index():
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)))
 
3
  from huggingface_hub import InferenceClient
4
  import os
5
 
6
+ # Load env variables
7
  load_dotenv()
8
 
9
+ # Initialize client
10
  client = InferenceClient(
11
  provider="hf-inference",
12
  api_key=os.getenv("HF_API_KEY")
 
14
 
15
  app = Flask(__name__)
16
 
17
+ # Blog generator
18
+ def generate_blog(topic):
19
  try:
20
+ prompt = f"Write a well-structured blog paragraph about: {topic}"
21
+
22
+ response = client.text_generation(
23
+ model="google/flan-t5-large",
24
+ prompt=prompt,
25
+ max_new_tokens=300,
 
 
 
26
  )
27
+
28
+ return response
29
 
30
  except Exception as e:
31
  return f"Error generating blog: {str(e)}"
32
 
33
 
 
34
  @app.route("/", methods=["GET", "POST"])
35
  def index():
36
  paragraph = ""
37
+
38
  if request.method == "POST":
39
  topic = request.form.get("topic")
40
  if topic:
 
43
  return render_template("index.html", paragraph=paragraph)
44
 
45
 
 
46
  if __name__ == "__main__":
47
  app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))