vinaysiddha commited on
Commit
d3a4db4
·
verified ·
1 Parent(s): 519d540

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -3
app.py CHANGED
@@ -1,6 +1,14 @@
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
 
 
4
  """
5
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
  """
@@ -40,6 +48,18 @@ def respond(
40
  yield response
41
 
42
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
  """
@@ -59,6 +79,7 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
1
+ from flask import Flask, request, jsonify
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
+ import torch
4
  import gradio as gr
5
  from huggingface_hub import InferenceClient
6
 
7
+ app = Flask(__name__)
8
+
9
+ tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-cnn-12-6")
10
+ model = AutoModelForSeq2SeqLM.from_pretrained("sshleifer/distilbart-cnn-12-6")
11
+
12
  """
13
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
14
  """
 
48
  yield response
49
 
50
 
51
+ @app.route('/summarize', methods=['POST'])
52
+ def summarize():
53
+ data = request.json
54
+ text = data.get('text', '')
55
+ if not text:
56
+ return jsonify({'error': 'No text provided'}), 400
57
+
58
+ inputs = tokenizer([text], max_length=1024, return_tensors="pt", truncation=True)
59
+ summary_ids = model.generate(inputs["input_ids"], max_length=60, min_length=10, length_penalty=2.0, num_beams=4, early_stopping=True)
60
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
61
+ return jsonify({'summary': summary})
62
+
63
  """
64
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
65
  """
 
79
  ],
80
  )
81
 
82
+ if __name__ == '__main__':
83
+ from waitress import serve
84
+ serve(app, host='0.0.0.0', port=5005)
85
+ demo.launch()