Spaces:
Sleeping
Sleeping
File size: 2,668 Bytes
3fdead9 3be0237 a7d7295 5bf9849 51c2df6 5bf9849 dbe832f 3be0237 51c2df6 2a2ac0b 51c2df6 3fb5ba6 2a2ac0b 51c2df6 3fb5ba6 51c2df6 2a2ac0b 2463937 3be0237 f979ae8 51c2df6 f979ae8 51c2df6 4f05ca4 f979ae8 51c2df6 f979ae8 3be0237 4f05ca4 |
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 |
import gradio as gr
import os
from langchain.chains.question_answering import load_qa_chain
from langchain.document_loaders import UnstructuredURLLoader
from langchain import OpenAI
from langchain import HuggingFaceHub
os.environ[
"HUGGINGFACEHUB_API_TOKEN"] = "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"
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
os.environ["LANGCHAIN_API_KEY"] = "ls__ae9b316f4ee9475b84f66c616344d713"
os.environ["LANGCHAIN_PROJECT"] = "Sequential-Chain"
def main():
input_api_key = gr.inputs.Textbox(label="ChatGPT API Key", lines=1)
input_api_base = gr.inputs.Textbox(label="ChatGPT API 地址(默认无地址)", lines=1)
input_url = gr.inputs.Textbox(label="URL", lines=1)
outputs = gr.outputs.Textbox(label="输出")
with gr.Blocks() as demo:
with gr.Tab("Component 1"): #标签页1
interface1 = gr.Interface(fn=my_inference_function,
inputs=[input_url],
outputs=outputs)
demo.add(interface1, "component1")
with gr.Tab("Component 2"): #标签页2
interface2 = gr.Interface(
fn=my_chatgpt_function,
inputs=[input_api_key, input_api_base, input_url],
outputs=outputs)
demo.add(interface2, "component2")
demo.launch()
def my_chatgpt_function(api_key, api_base, url):
os.environ["OPENAI_API_KEY"] = api_key
os.environ['OPENAI_API_BASE'] = api_base
llm = OpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=1024)
loader = UnstructuredURLLoader(urls=[url])
data = loader.load()
chain = load_qa_chain(llm=llm, chain_type="stuff")
response = chain.run(input_documents=data,
question="""请用中文总结文章的内容,并以下面模版给出结果:
《文章标题》摘要如下:
## 一句话描述
文章摘要内容
## 文章略读
文章要点""")
return response
def my_inference_function(url):
llm = HuggingFaceHub(repo_id="declare-lab/flan-alpaca-large",
model_kwargs={
"temperature": 0.1,
"max_length": 512
})
loader = UnstructuredURLLoader(urls=[url])
data = loader.load()
chain = load_qa_chain(llm=llm, chain_type="stuff")
response = chain.run(input_documents=data,
question="Summarize this article in one paragraph")
return response
if __name__ == '__main__':
main()
|