CoolFace
Apppublic

chmodsss/WebChat

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py117 linesDownload Raw Back to root
1import openai2import logging3import nltkmodules4import gradio as gr5 6from langchain import OpenAI7from llama_index.readers import Document8from llama_index import GPTSimpleVectorIndex, LLMPredictor, PromptHelper9from urllib import request10from urllib.error import HTTPError11from bs4 import BeautifulSoup as bs12from nltk import word_tokenize13 14 15logging.basicConfig(16    format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)17logger = logging.getLogger(__name__)18 19models_list = ['text-ada-001', 'text-curie-001', 'text-babbage-001']20global model, index, temperature21model = models_list[0]22temperature = 023 24def get_url_data(url):25    global model26    global index27    global temperature28    try:29        page = request.urlopen(url).read()30        content = bs(page).get_text()31        content_tokenized = ' '.join(word_tokenize(content))32        logger.info("Page read..")33    except HTTPError as err:34        logger.error("Invalid URL")35 36    max_input_size = 409637    num_outputs = 25638    max_chunk_overlap = 2039    chunk_size_limit = 60040    prompt_helper = PromptHelper(max_input_size=max_input_size, num_output=num_outputs,41                                 max_chunk_overlap=max_chunk_overlap, chunk_size_limit=chunk_size_limit)42 43    logger.info(f"model found :{model}")44    logger.info(f"api key :{openai.api_key}")45    llm_predictor = LLMPredictor(llm=OpenAI(46        openai_api_key=openai.api_key, temperature=temperature, model_name=model, max_tokens=num_outputs))47 48    index = GPTSimpleVectorIndex([Document(49        content_tokenized)], llm_predictor=llm_predictor, prompt_helper=prompt_helper)50 51    return f"index created for the article: \"{url}\""52 53 54def predict(history, query):55    global index56    history = history or []57    print("index found ", index)58    result = index.query(query, response_mode="compact")59    logger.info(result.response)60    history = history + [(query, result.response)]61    return history62 63def set_api_key(key):64    openai.api_key = key65    return "API key loaded..."66 67def set_model(sel_model):68    global model69    model = sel_model70    return "Chosen model: " + model71 72 73def set_temperature(sel_temperature):74    global temperature75    temperature = sel_temperature76    return None77 78 79demo = gr.Blocks(css="#chatbot .overflow-y-auto{height:500px}")80with demo:81    with gr.Tab(label='Chatbot'):82        with gr.Row():83            with gr.Column(scale=0.85):84                url = gr.Textbox(show_label=False, placeholder="Enter the URL here... ").style(85                    container=False)86            with gr.Column(scale=0.15, min_width=0):87                send_url = gr.Button('Load')88        idx_display = gr.Textbox(show_label=False).style(89            container=False, border=True)90        url.submit(get_url_data, inputs=[url], outputs=[idx_display])91        send_url.click(get_url_data, inputs=[url], outputs=[idx_display])92 93        chatbot = gr.Chatbot(elem_id="chatbot").style(height=300)94        with gr.Row():95            with gr.Column(scale=0.85):96                msg = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(97                    container=False)98            with gr.Column(scale=0.15, min_width=0):99                send_chat = gr.Button('Send')100        state = gr.State()101        msg.submit(predict, [chatbot, msg], chatbot)102        msg.submit(lambda: "", None, msg)103        send_chat.click(predict, [chatbot, msg], chatbot)104 105    with gr.Tab(label='Settings'):106        sel_api = gr.Textbox(label="OPENAI_API_KEY", placeholder="Enter OpenAI API key here...")107        sel_model = gr.Dropdown(label='Select the Model',108                                choices=models_list, value=models_list[0])109        sel_temperature = gr.Slider(0, 1, value=0, step=0.1, label='Set the temperature')110        api_key_status = gr.Markdown("No API key given...")111        model_status = gr.Markdown("Chosen Model: text-ada-001 (default)")112        sel_api.submit(set_api_key, sel_api, api_key_status)113        sel_model.change(set_model, sel_model, model_status)114        sel_temperature.change(set_temperature, sel_temperature, None)115 116demo.launch()117