import flask from flask import Flask, request, jsonify from langchain.document_loaders import WebBaseLoader from langchain.chains.summarize import load_summarize_chain from bs4 import BeautifulSoup import os from dotenv import load_dotenv load_dotenv() from langchain import HuggingFaceHub from huggingface_hub import InferenceClient import requests import sys hf_token = os.environ.get('HUGGINGFACEHUB_API_TOKEN') #starchat_repo_id = os.environ.get('starchat_repo_id') #repo_id=os.environ.get('repo_id') #port = os.getenv('port') llm = HuggingFaceHub(repo_id=starchat_repo_id, #for StarChat huggingfacehub_api_token=hf_token, model_kwargs={"min_length":512, #for StarChat "max_new_tokens":1024, "do_sample":True, #for StarChat "temperature":0.01, "top_k":50, "top_p":0.95, "eos_token_id":49155}) chain = load_summarize_chain(llm, chain_type="stuff") app = Flask(__name__) @app.route('/', methods=['POST']) def home_api(): data = request.get_json() user_query = data['user_question'] print(user_query) return {"Message":"Flask Home API Deploy Success on HF"} @app.route('/api/chat', methods=['POST']) def chat(): #async def chat(): #Failed - Flask API 不支持async??? data = request.get_json() user_query = data['user_question'] print(user_query) if user_query !="" and not user_query.strip().isspace() and not user_query == "" and not user_query.strip() == "" and not user_query.isspace(): try: loader = WebBaseLoader(user_query) print(user_query) docs = loader.load() result=chain.run(docs) print("AI Summarization: "+result) return jsonify({'response': result}) except Exception as e: err_msg="Wrong URL or URL not parsable." print(err_msg) return jsonify({'response': err_msg})