File size: 3,165 Bytes
6ca8627 cacdd22 b324113 e501604 1570310 6520b77 a871520 6520b77 0252582 6520b77 cacdd22 58504c7 6520b77 b324113 41a17c2 4553156 ac0d3d4 4553156 41a17c2 7ca0615 41a17c2 8e2baa5 7724647 cacdd22 8e2baa5 41a17c2 f6d07d5 41a17c2 ac0d3d4 ab56333 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import streamlit as st
#from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import WebBaseLoader
from langchain.chains.summarize import load_summarize_chain
from bs4 import BeautifulSoup #WebBaseLoader会需要 用到?
from langchain import HuggingFaceHub
import requests
import sys
from huggingface_hub import InferenceClient
import os
from dotenv import load_dotenv
load_dotenv()
hf_token = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
repo_id=os.environ.get('repo_id')
#port = os.getenv('port')
#OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
llm = HuggingFaceHub(repo_id=repo_id, # for StarChat
huggingfacehub_api_token=hf_token, #这个变量huggingfacehub_api_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") #stuff模式容易导致出错:估计是超LLM的token限制所致
chain = load_summarize_chain(llm, chain_type="refine")
#llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-16k")
#**************************************************************#
#url=st.text_input("Enter webiste URL to summarize (format: https://www.usinoip.com):")
#loader = WebBaseLoader("https://www.usinoip.com/")
#loader = WebBaseLoader(url)
#with st.spinner("AI Thinking...Please wait a while to Cheers!"):
# docs = loader.load()
# result=chain.run(docs)
# print(result)
# st.write("AI Summarization:")
# st.write(result)
#**************************************************************#
#**************************************************************#
url=st.text_input("Enter webiste URL to summarize (format: https://www.usinoip.com):")
#loader = WebBaseLoader("https://www.usinoip.com/")
if url !="" and not url.strip().isspace() and not url == "" and not url.strip() == "" and not url.isspace():
try:
loader = WebBaseLoader(url)
with st.spinner("AI Thinking...Please wait a while to Cheers!"):
docs = loader.load()
result=chain.run(docs) #这个result的格式比较特殊,可以直接print,但不可以和其他字符串联合print输出
print(url)
#print("AI Summarization: "+result) #这个会出错,原因见上方
print("AI Summarization:")
print(result)
st.write("AI Summarization:")
st.write(result)
except Exception as e:
st.write("Wrong URL or URL not parsable.")
#**************************************************************#
#try:
# loader = WebBaseLoader(url)
# with st.spinner("AI Thinking...Please wait a while to Cheers!"):
# docs = loader.load()
# result=chain.run(docs)
# print(result)
# st.write("AI Summarization:")
# st.write(result)
#except Exception as e:
# st.write("Wrong URL") |