parkerjj commited on
Commit
d794995
1 Parent(s): 6288b1a

First Commit for test Hugging face platform

Browse files
Files changed (1) hide show
  1. app.py +35 -15
app.py CHANGED
@@ -1,27 +1,47 @@
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('请输入文本!')
 
 
 
 
 
 
1
  import requests
2
+ import streamlit as st
3
+ from flask import Flask, request, jsonify
4
+ import threading
5
+
6
+ # Flask 部分
7
+ flask_app = Flask(__name__)
8
 
9
+ @flask_app.route('/predict', methods=['POST'])
10
+ def predict():
11
+ data = request.get_json()
12
+ if not data or 'input' not in data:
13
+ return jsonify({'error': 'Invalid input'}), 400
14
+ input_text = data['input']
15
+ # 模拟模型预测
16
+ result = {"prediction": f"Predicted for: {input_text}"}
17
+ return jsonify(result)
18
+
19
+ def run_flask():
20
+ flask_app.run(port=5000, debug=False)
21
+
22
+ # Streamlit 部分
23
+ st.title("AI Model API Interface")
24
+ st.write("输入文本,获取模型预测结果。")
25
 
26
  # 用户输入
27
+ user_input = st.text_area("请输入文本:")
28
 
29
+ if st.button("提交"):
30
  if user_input.strip():
31
+ with st.spinner("正在预测..."):
32
  try:
33
+ api_url = "http://127.0.0.1:5000/predict" # 内部调用 Flask
34
+ response = requests.post(api_url, json={"input": user_input})
 
 
35
  if response.status_code == 200:
36
  prediction = response.json()
37
+ st.success(f"预测结果: {prediction}")
38
  else:
39
+ st.error(f"错误: {response.text}")
40
  except Exception as e:
41
+ st.error(f"请求失败: {e}")
42
  else:
43
+ st.warning("请输入文本!")
44
+
45
+ # 使用线程运行 Flask
46
+ if __name__ == "__main__":
47
+ threading.Thread(target=run_flask).start()