binqiangliu commited on
Commit
72eb71d
·
1 Parent(s): 71b18ab

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import streamlit as st
3
+ from langchain import PromptTemplate, LLMChain
4
+ from langchain.memory import StreamlitChatMessageHistory
5
+ from streamlit_chat import message
6
+ import numpy as np
7
+ from langchain.chains import LLMChain
8
+ from langchain.prompts import PromptTemplate
9
+ from langchain.memory import ConversationBufferMemory
10
+ from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
11
+ from streamlit.components.v1 import html
12
+ from langchain import HuggingFaceHub
13
+ import os
14
+ from dotenv import load_dotenv
15
+ load_dotenv()
16
+ from pathlib import Path
17
+ from huggingface_hub import InferenceClient
18
+ from langchain import HuggingFaceHub
19
+ import requests
20
+ #from time import sleep
21
+ import uuid
22
+ import sys
23
+ from streamlit_extras.colored_header import colored_header
24
+ from streamlit_extras.add_vertical_space import add_vertical_space
25
+
26
+ st.set_page_config(page_title="AI Chatbot 100% Free", layout="wide")
27
+ st.write('完全开源免费的AI智能聊天助手 | Absolute Free & Opensouce AI Chatbot')
28
+
29
+ css_file = "main.css"
30
+ with open(css_file) as f:
31
+ st.markdown("<style>{}</style>".format(f.read()), unsafe_allow_html=True)
32
+
33
+ # 初始化Chatbot
34
+ HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
35
+ repo_id = os.environ.get('repo_id')
36
+ port = os.getenv('port')
37
+
38
+ llm = HuggingFaceHub(repo_id=repo_id,
39
+ model_kwargs={"min_length":1024,
40
+ "max_new_tokens":5632, "do_sample":True,
41
+ "temperature":0.1,
42
+ "top_k":50,
43
+ "top_p":0.95, "eos_token_id":49155})
44
+
45
+ #prompt_template = """You are a very helpful AI assistant. Please response to the user's input question with as many details as possible.
46
+ #Question: {user_question}
47
+ #Helpful AI Repsonse:
48
+ #"""
49
+
50
+ prompt_template = """
51
+ <<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.
52
+ 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.
53
+ In each conversation, question is placed after [INST] while your answer should be placed after [/INST].<</SYS>>
54
+ [INST] {user_question} [/INST]
55
+ assistant:
56
+ """
57
+
58
+ llm_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template(prompt_template))
59
+
60
+ temp_user_query = st.chat_input("Enter your question here.")
61
+
62
+ # 定义API端点
63
+ app = Flask(__name__)
64
+ @app.route('/api/chat', methods=['POST'])
65
+ def chat():
66
+ data = request.get_json()
67
+ #user_query = data['query']
68
+ #此处的['query']中的query可以自定义名称,例如修改为user_question,那么调用API的代码中,需要相应的使用data = {'user_question': user_query},user_question需一致
69
+ user_query = data['user_question']
70
+ temp_user_query=user_query
71
+ # 调用Chatbot
72
+ initial_response = llm_chain.run(user_query)
73
+ st.write("AI Response")
74
+ st.write(initial_response)
75
+ return jsonify({'response': initial_response})
76
+
77
+ if __name__ == '__main__':
78
+ #app.run(host='0.0.0.0', port=port)
79
+ app.run(host='0.0.0.0')