Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
from langchain.chat_models import ChatOpenAI
|
| 3 |
from langchain.document_loaders import WebBaseLoader
|
| 4 |
from langchain.chains.summarize import load_summarize_chain
|
|
@@ -30,35 +32,58 @@ llm = HuggingFaceHub(repo_id=repo_id, #for Llama2
|
|
| 30 |
#llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-16k")
|
| 31 |
chain = load_summarize_chain(llm, chain_type="stuff")
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# print(result)
|
| 41 |
-
# st.write("AI Summarization:")
|
| 42 |
-
# st.write(result)
|
| 43 |
-
#**************************************************************#
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
if
|
| 51 |
try:
|
| 52 |
-
loader = WebBaseLoader(
|
| 53 |
with st.spinner("AI Thinking...Please wait a while to Cheers!"):
|
| 54 |
docs = loader.load()
|
| 55 |
result=chain.run(docs)
|
| 56 |
-
print(
|
| 57 |
print("AI Summarization: "+result)
|
| 58 |
-
st.write("AI Summarization:")
|
| 59 |
-
st.write(result)
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
-
st.write("Wrong URL or URL not parsable.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
#**************************************************************#
|
| 63 |
|
| 64 |
#try:
|
|
|
|
| 1 |
+
import flask
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
#import streamlit as st
|
| 4 |
from langchain.chat_models import ChatOpenAI
|
| 5 |
from langchain.document_loaders import WebBaseLoader
|
| 6 |
from langchain.chains.summarize import load_summarize_chain
|
|
|
|
| 32 |
#llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-16k")
|
| 33 |
chain = load_summarize_chain(llm, chain_type="stuff")
|
| 34 |
|
| 35 |
+
app = Flask(__name__)
|
| 36 |
+
@app.route('/', methods=['POST'])
|
| 37 |
+
def home_api():
|
| 38 |
+
data = request.get_json()
|
| 39 |
+
user_query = data['user_question']
|
| 40 |
+
print(user_query)
|
| 41 |
+
return {"Message":"Flask Home API Deploy Success on HF"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
@app.route('/api/chat', methods=['POST'])
|
| 44 |
+
def chat():
|
| 45 |
+
#async def chat(): #Failed - Flask API 不支持async???
|
| 46 |
+
data = request.get_json()
|
| 47 |
+
user_query = data['user_question']
|
| 48 |
+
if user_query !="" and not user_query.strip().isspace() and not user_query == "" and not user_query.strip() == "" and not user_query.isspace():
|
| 49 |
try:
|
| 50 |
+
loader = WebBaseLoader(user_query)
|
| 51 |
with st.spinner("AI Thinking...Please wait a while to Cheers!"):
|
| 52 |
docs = loader.load()
|
| 53 |
result=chain.run(docs)
|
| 54 |
+
print(user_query)
|
| 55 |
print("AI Summarization: "+result)
|
| 56 |
+
#st.write("AI Summarization:")
|
| 57 |
+
#st.write(result)
|
| 58 |
+
return jsonify({'response': result})
|
| 59 |
except Exception as e:
|
| 60 |
+
#st.write("Wrong URL or URL not parsable.")
|
| 61 |
+
err_msg="Wrong URL or URL not parsable."
|
| 62 |
+
return jsonify({'response': err_msg})
|
| 63 |
+
|
| 64 |
+
#initial_response = llm_chain.run(user_query)
|
| 65 |
+
#return jsonify({'response': initial_response})
|
| 66 |
+
#找到问题了:jsonify在Huggingface不支持;在Github然后部署到Render是可以的!---NO No No, it's supported
|
| 67 |
+
#return {'response': initial_response}
|
| 68 |
+
#return jsonify({'response': initial_response}) #tried and OKed!
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
#url=st.text_input("Enter webiste URL to summarize (format: https://www.usinoip.com):")
|
| 72 |
+
|
| 73 |
+
#loader = WebBaseLoader("https://www.usinoip.com/")
|
| 74 |
+
|
| 75 |
+
#if url !="" and not url.strip().isspace() and not url == "" and not url.strip() == "" and not url.isspace():
|
| 76 |
+
# try:
|
| 77 |
+
# loader = WebBaseLoader(url)
|
| 78 |
+
# with st.spinner("AI Thinking...Please wait a while to Cheers!"):
|
| 79 |
+
# docs = loader.load()
|
| 80 |
+
# result=chain.run(docs)
|
| 81 |
+
# print(url)
|
| 82 |
+
# print("AI Summarization: "+result)
|
| 83 |
+
# st.write("AI Summarization:")
|
| 84 |
+
# st.write(result)
|
| 85 |
+
# except Exception as e:
|
| 86 |
+
# st.write("Wrong URL or URL not parsable.")
|
| 87 |
#**************************************************************#
|
| 88 |
|
| 89 |
#try:
|