Erfan11 commited on
Commit
ff60146
1 Parent(s): d76f6e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -46
app.py CHANGED
@@ -1,53 +1,34 @@
1
- import os
2
- import requests
3
- import yaml
4
  from flask import Flask, request, jsonify
5
- from dotenv import load_dotenv
6
- from PIL import Image
7
- import io
8
  from transformers import pipeline
9
 
10
- # Load environment variables
11
- load_dotenv()
12
- api_key = os.getenv('HF_API_KEY')
13
- model_path = os.getenv('MODEL_PATH')
14
-
15
  app = Flask(__name__)
16
 
17
- # Load configuration
18
- with open('config.yaml', 'r') as file:
19
- config = yaml.safe_load(file)
20
-
21
- def get_model_predictions(text):
22
- headers = {"Authorization": f"Bearer {api_key}"}
23
- payload = {"inputs": text}
24
- response = requests.post(f"https://api-inference.huggingface.co/models/{model_path}", headers=headers, json=payload)
25
- return response.json()
26
-
27
- def process_image(image_file):
28
- image = Image.open(image_file)
29
- # Implement image processing here
30
- return "Image processed"
31
-
32
- @app.route('/predict', methods=['POST'])
33
- def predict():
34
- data = request.get_json()
35
- text = data.get('text')
36
- image_file = data.get('image')
37
-
38
- if text:
39
- prediction = get_model_predictions(text)
40
- return jsonify(prediction)
41
- elif image_file:
42
- image = io.BytesIO(image_file)
43
- result = process_image(image)
44
- return jsonify({"result": result})
45
- else:
46
- return jsonify({"error": "No input provided"})
47
-
48
- @app.route('/', methods=['GET'])
49
- def index():
50
- return "Welcome to My AI! Use /predict to interact."
51
 
52
  if __name__ == '__main__':
53
- app.run(debug=True, use_reloader=False)
 
 
 
 
1
  from flask import Flask, request, jsonify
 
 
 
2
  from transformers import pipeline
3
 
 
 
 
 
 
4
  app = Flask(__name__)
5
 
6
+ # Initialize the text generation pipeline
7
+ generator = pipeline('text-generation', model='gpt2') # Replace 'gpt2' with your model
8
+
9
+ @app.route('/')
10
+ def home():
11
+ return '''
12
+ <html>
13
+ <body>
14
+ <h1>Text Generation</h1>
15
+ <form action="/generate" method="post">
16
+ <textarea name="text_input" rows="4" cols="50" placeholder="Enter text here..."></textarea><br>
17
+ <input type="submit" value="Generate">
18
+ </form>
19
+ </body>
20
+ </html>
21
+ '''
22
+
23
+ @app.route('/generate', methods=['POST'])
24
+ def generate():
25
+ text_input = request.form['text_input']
26
+ if text_input:
27
+ # Generate text using the model
28
+ result = generator(text_input, max_length=100, num_return_sequences=1)
29
+ generated_text = result[0]['generated_text']
30
+ return jsonify({'generated_text': generated_text})
31
+ return jsonify({'error': 'No input provided'}), 400
 
 
 
 
 
 
 
 
32
 
33
  if __name__ == '__main__':
34
+ app.run(debug=True)