Erfan11 commited on
Commit
c906021
1 Parent(s): 1a1063b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -10
app.py CHANGED
@@ -1,25 +1,53 @@
1
- import streamlit as st
2
- import requests
3
  import os
 
 
 
4
  from dotenv import load_dotenv
 
 
 
5
 
 
6
  load_dotenv()
7
  api_key = os.getenv('HF_API_KEY')
8
  model_path = os.getenv('MODEL_PATH')
9
 
 
 
 
 
 
 
10
  def get_model_predictions(text):
11
  headers = {"Authorization": f"Bearer {api_key}"}
12
  payload = {"inputs": text}
13
- response = requests.post(f"https://api.huggingface.co/models/{model_path}", headers=headers, json=payload)
14
  return response.json()
15
 
16
- st.title("My AI Prediction App")
 
 
 
17
 
18
- text = st.text_area("Enter text to predict")
19
-
20
- if st.button("Predict"):
 
 
 
21
  if text:
22
- result = get_model_predictions(text)
23
- st.write("Prediction:", result)
 
 
 
 
24
  else:
25
- st.write("Please enter some text.")
 
 
 
 
 
 
 
 
 
 
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)