Spaces:
Running
Running
First Commit for test Hugging face platform
Browse files- app/__init__.py +7 -0
- app/model.py +11 -0
- app/routes.py +12 -0
- config.py +3 -0
- requirements.txt +3 -0
- streamlit_app.py +27 -0
app/__init__.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask
|
2 |
+
|
3 |
+
def create_app():
|
4 |
+
app = Flask(__name__)
|
5 |
+
from .routes import api
|
6 |
+
app.register_blueprint(api)
|
7 |
+
return app
|
app/model.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
|
3 |
+
# 加载模型
|
4 |
+
#model = pipeline('text-classification')
|
5 |
+
|
6 |
+
def predict(text):
|
7 |
+
try:
|
8 |
+
result = text + ' is a good movie'
|
9 |
+
return result
|
10 |
+
except Exception as e:
|
11 |
+
return {'error': str(e)}
|
app/routes.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Blueprint, request, jsonify
|
2 |
+
from .model import predict
|
3 |
+
|
4 |
+
api = Blueprint('api', __name__)
|
5 |
+
|
6 |
+
@api.route('/predict', methods=['POST'])
|
7 |
+
def predict_route():
|
8 |
+
data = request.get_json()
|
9 |
+
if not data or 'input' not in data:
|
10 |
+
return jsonify({'error': 'Invalid input'}), 400
|
11 |
+
prediction = predict(data['input'])
|
12 |
+
return jsonify({'prediction': prediction})
|
config.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
class Config:
|
2 |
+
DEBUG = True
|
3 |
+
SECRET_KEY = 'your-secret-key'
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
streamlit
|
3 |
+
transformers
|
streamlit_app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# Streamlit 页面设置
|
5 |
+
st.title('AI Model API Interface')
|
6 |
+
st.write('输入文本,获取模型预测结果。')
|
7 |
+
|
8 |
+
# 用户输入
|
9 |
+
user_input = st.text_area('请输入文本:')
|
10 |
+
|
11 |
+
if st.button('提交'):
|
12 |
+
if user_input.strip():
|
13 |
+
with st.spinner('正在预测...'):
|
14 |
+
try:
|
15 |
+
response = requests.post(
|
16 |
+
'http://127.0.0.1:5000/predict',
|
17 |
+
json={'input': user_input}
|
18 |
+
)
|
19 |
+
if response.status_code == 200:
|
20 |
+
prediction = response.json()
|
21 |
+
st.success(f'预测结果: {prediction}')
|
22 |
+
else:
|
23 |
+
st.error(f'错误: {response.text}')
|
24 |
+
except Exception as e:
|
25 |
+
st.error(f'请求失败: {e}')
|
26 |
+
else:
|
27 |
+
st.warning('请输入文本!')
|