Spaces:
Runtime error
Runtime error
Zwea Htet commited on
Commit ·
3b7cf58
1
Parent(s): 8fe964e
fixed some bugs
Browse filesFiles changed (3)
- app.py+14-38
- models/bloom.py+15-15
- utils/customLLM.py+17-5
1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162631 unmodified lineimport requestsimport streamlit as stfrom models import bloomst.title("Welcome to RegBotBeta")st.header("Powered by `LlamaIndex🦙` and `OpenAI API`")def validate(token: str):api_endpoint = "https://api.openai.com/v1/chat/completions"api_key = tokenheaders = {"Content-Type" : "application/json","Authorization": f"Bearer {api_key}"}messages = [{"role": "user", "content": "Say this is a test!"}]data = {"model": "gpt-3.5-turbo","messages": messages}response = requests.post(api_endpoint, json=data, headers=headers)return responsedef create_index():index = bloom.initialize_index("bloomLlama")return indexdef get_response(vector_index, query_str):query_engine = vector_index.as_query_engine()response = query_engine.query(query_str)return responseapi_key = st.text_input("Enter your OpenAI API key here:", type="password")if api_key:resp = validate(api_key)if ("error" in resp.json()):st.info("Your API Token is incorrect! Try again.")else:os.environ["OPENAI_API_KEY"] = api_keyindex = create_index()st.write("---")input_text = st.text_area("Ask your question")if input_text is not None:if st.button("Ask"):st.info("Your query: \n" + input_text)with st.spinner("Processing your query..."):response = get_response(index, input_text)print(response)st.success(response)st.write("---")1 unmodified line234567891011121314151617181920212223242526272829303132333435363738391 unmodified lineimport requestsimport streamlit as stfrom streamlit_chat import messagefrom models import bloomfrom utils.util import *st.title("Welcome to RegBotBeta")st.header("Powered by `LlamaIndex🦙` and `OpenAI API`")if 'messages' not in st.session_state:st.session_state.messages = []api_key = st.text_input("Enter your OpenAI API key here:", type="password")if api_key:resp = validate(api_key)if ("error" in resp.json()):st.info("Invalid Token! Try again.")else:st.info("Success")os.environ["OPENAI_API_KEY"] = api_keyindex = create_index(bloom)st.write("---")input_text = st.text_area("Ask your question")if input_text is not None:if st.button("Ask"):st.session_state.messages.append(('User', input_text))with st.spinner("Processing your query..."):bot_response = get_response(index, input_text)st.session_state.messages.append(('Bot', bot_response))# Display previous messagesfor sender, msg in st.session_state.messages[::-1]:message(msg, is_user = (sender=='User'))
13 unmodified lines1415161718192021225 unmodified lines282930313233343536373839404142434445464713 unmodified linesload_dotenv()# get modelmodel_name = "bigscience/bloom-560m"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name, config='T5Config')# define prompt helper# set maximum input size5 unmodified linesprompt_helper = PromptHelper(context_window, num_output, chunk_overlap_ratio)# create a pipelinepl = pipeline(model=model,tokenizer=tokenizer,task="text-generation",# device=0, # GPU device number# max_length=512,do_sample=True,top_p=0.95,top_k=50,temperature=0.7)# define llmllm_predictor = LLMPredictor(llm=CustomLLM(model_pipeline=pl))service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)def prepare_data(file_path:str):13 unmodified lines1415161718192021225 unmodified lines282930313233343536373839404142434445464713 unmodified linesload_dotenv()# get model# model_name = "bigscience/bloom-560m"# tokenizer = AutoTokenizer.from_pretrained(model_name)# model = AutoModelForCausalLM.from_pretrained(model_name, config='T5Config')# define prompt helper# set maximum input size5 unmodified linesprompt_helper = PromptHelper(context_window, num_output, chunk_overlap_ratio)# create a pipeline# pl = pipeline(# model=model,# tokenizer=tokenizer,# task="text-generation",# # device=0, # GPU device number# # max_length=512,# do_sample=True,# top_p=0.95,# top_k=50,# temperature=0.7# )# define llmllm_predictor = LLMPredictor(llm=CustomLLM())service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)def prepare_data(file_path:str):
1234567891011121314from typing import Any, List, Mapping, Optionalfrom langchain.llms.base import LLMfrom transformers import Pipelineclass CustomLLM(LLM):pipeline = None# Create the pipeline for question answeringdef __init__(self, model_pipeline: Pipeline):self.pipeline = model_pipelinedef _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:prompt_length = len(prompt)1234567891011121314151617181920212223242526from typing import Any, List, Mapping, Optionalfrom langchain.llms.base import LLMfrom transformers import AutoModelForCausalLM, AutoTokenizer, pipelinemodel_name = "bigscience/bloom-560m"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name, config='T5Config')pl = pipeline(model=model,tokenizer=tokenizer,task="text-generation",# device=0, # GPU device number# max_length=512,do_sample=True,top_p=0.95,top_k=50,temperature=0.7)class CustomLLM(LLM):pipeline = pldef _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:prompt_length = len(prompt)
app.py+14-38
models/bloom.py+15-15
utils/customLLM.py+17-5