Spaces:
Runtime error
Runtime error
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import flask
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
from langchain import PromptTemplate, LLMChain
|
| 4 |
+
from langchain.chains import LLMChain
|
| 5 |
+
from langchain.prompts import PromptTemplate
|
| 6 |
+
from langchain.memory import ConversationBufferMemory
|
| 7 |
+
from langchain import HuggingFaceHub
|
| 8 |
+
import os
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
load_dotenv()
|
| 11 |
+
from huggingface_hub import InferenceClient
|
| 12 |
+
from langchain import HuggingFaceHub
|
| 13 |
+
import requests
|
| 14 |
+
import uuid
|
| 15 |
+
import sys
|
| 16 |
+
|
| 17 |
+
hf_token = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
|
| 18 |
+
repo_id = os.environ.get('repo_id')
|
| 19 |
+
#port = os.getenv('port')
|
| 20 |
+
|
| 21 |
+
llm = HuggingFaceHub(repo_id=repo_id,
|
| 22 |
+
#huggingfacehub_api_token="hf_p***K",
|
| 23 |
+
huggingfacehub_api_token=hf_token,
|
| 24 |
+
model_kwargs={"min_length":1024,
|
| 25 |
+
"max_new_tokens":5632, "do_sample":True,
|
| 26 |
+
"temperature":0.1,
|
| 27 |
+
"top_k":50,
|
| 28 |
+
"top_p":0.95, "eos_token_id":49155})
|
| 29 |
+
|
| 30 |
+
prompt_template = """
|
| 31 |
+
<<SYS>>You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
| 32 |
+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
| 33 |
+
In each conversation, question is placed after [INST] while your answer should be placed after [/INST].<</SYS>>
|
| 34 |
+
[INST] {user_question} [/INST]
|
| 35 |
+
assistant:
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
llm_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template(prompt_template))
|
| 39 |
+
|
| 40 |
+
app = Flask(__name__)
|
| 41 |
+
@app.route('/', methods=['POST'])
|
| 42 |
+
def home_api():
|
| 43 |
+
data = request.get_json()
|
| 44 |
+
user_query = data['user_question']
|
| 45 |
+
print(user_query)
|
| 46 |
+
return {"Hey":"Flask Home API Deploy Success on HF"}
|
| 47 |
+
|
| 48 |
+
@app.route('/api/chat', methods=['POST'])
|
| 49 |
+
def chat():
|
| 50 |
+
#async def chat():
|
| 51 |
+
#不支持async
|
| 52 |
+
data = request.get_json()
|
| 53 |
+
user_query = data['user_question']
|
| 54 |
+
initial_response = llm_chain.run(user_query)
|
| 55 |
+
#return jsonify({'response': initial_response})
|
| 56 |
+
#找到问题了:jsonify在Huggingface不支持;在Github然后部署到Render是可以的!
|
| 57 |
+
return {'response': initial_response}
|