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 line2345678910111211121314151617181920212223242526272829303132333435363738394013141516171819462021222349242526272829305631325859606133343536633738391 unmodified line
import requestsimport streamlit as stfrom streamlit_chat import message
from models import bloomfrom utils.util import *
st.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 = token
headers = { "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 response
def create_index(): index = bloom.initialize_index("bloomLlama") return index
def get_response(vector_index, query_str): query_engine = vector_index.as_query_engine() response = query_engine.query(query_str) return response
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("Your API Token is incorrect! Try again.") st.info("Invalid Token! Try again.") else: st.info("Success") os.environ["OPENAI_API_KEY"] = api_key index = create_index() index = create_index(bloom)
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) st.session_state.messages.append(('User', input_text)) with st.spinner("Processing your query..."): response = get_response(index, input_text) print(response)
st.success(response) bot_response = get_response(index, input_text) st.session_state.messages.append(('Bot', bot_response))
st.write("---")# Display previous messagesfor sender, msg in st.session_state.messages[::-1]: message(msg, is_user = (sender=='User'))13 unmodified lines1415161718191718192021225 unmodified lines282930313233343536373839404131323334353637383940414243444445464713 unmodified linesload_dotenv()
# get modelmodel_name = "bigscience/bloom-560m" tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name, config='T5Config')# 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 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)# 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(model_pipeline=pl))llm_predictor = LLMPredictor(llm=CustomLLM())service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
def prepare_data(file_path:str):12344567891011121314151617181920212289101123242526from typing import Any, List, Mapping, Optional
from langchain.llms.base import LLMfrom transformers import Pipelinefrom transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
model_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 = None # Create the pipeline for question answering def __init__(self, model_pipeline: Pipeline): self.pipeline = model_pipeline pipeline = pl
def _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