Spaces:
Build error
Build error
Update app.py
Browse files12345678910111213141516171819202122232425262721 unmodified lines4950515253545556575859606162636465666768697071727374757677787921 unmodified lines101102103104105106107import torchimport copyimport gradio as grimport spacesfrom llama_cpp import Llamaimport llama_cpp.llama_tokenizerimport osfrom huggingface_hub import hf_hub_downloadHF_TOKEN = os.environ.get("HF_TOKEN", None)MODEL_ID = "google/gemma-2-27b-it"REPO_ID = "bartowski/gemma-2-27b-it-GGUF"MODEL_NAME = MODEL_ID.split("/")[-1]MODEL_FILE = "gemma-2-27b-it-Q4_K_M.gguf"os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"llm = llama_cpp.Llama.from_pretrained(repo_id=REPO_ID,filename=MODEL_FILE,tokenizer=llama_cpp.llama_tokenizer.LlamaHFTokenizer.from_pretrained(MODEL_ID),verbose=False,)TITLE = "<h1><center>Chatbox</center></h1>"21 unmodified lines@spaces.GPU(duration=90)def stream_chat(message: str, history: list, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):print(f'message is - {message}')print(f'history is - {history}')conversation = []for prompt, answer in history:conversation.extend([{"role": "user", "content": prompt}, {"role": "assistant", "content": answer}])conversation.append({"role": "user", "content": message})print(f"Conversation is -\n{conversation}")output = llm(messages=conversation,top_k=top_k,top_p=top_p,repeat_penalty=penalty,max_tokens=max_new_tokens,stream =True,temperature=temperature,)for out in output:stream = copy.deepcopy(out)temp += stream["choices"][0]["text"]yield tempchatbot = gr.Chatbot(height=600)21 unmodified linesmaximum=2048,step=1,value=1024,label="Max Tokens",render=False,),gr.Slider(123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858621 unmodified lines10810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914021 unmodified lines162163164165166167168model_name = "gemma2:27b"import osos.system("sudo apt install lshw")os.system("curl https://ollama.ai/install.sh | sh")import nest_asyncionest_asyncio.apply()import osimport asyncio# Run Async Ollama# Taken from: https://stackoverflow.com/questions/77697302/how-to-run-ollama-in-google-colab# NB: You may need to set these depending and get cuda working depending which backend you are running.# Set environment variable for NVIDIA library# Set environment variables for CUDAos.environ['PATH'] += ':/usr/local/cuda/bin'# Set LD_LIBRARY_PATH to include both /usr/lib64-nvidia and CUDA lib directoriesos.environ['LD_LIBRARY_PATH'] = '/usr/lib64-nvidia:/usr/local/cuda/lib64'async def run_process(cmd):print('>>> starting', *cmd)process = await asyncio.create_subprocess_exec(*cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)# define an async pipe functionasync def pipe(lines):async for line in lines:print(line.decode().strip())await asyncio.gather(pipe(process.stdout),pipe(process.stderr),)# call itawait asyncio.gather(pipe(process.stdout), pipe(process.stderr))import asyncioimport threadingasync def start_ollama_serve():await run_process(['ollama', 'serve'])def run_async_in_thread(loop, coro):asyncio.set_event_loop(loop)loop.run_until_complete(coro)loop.close()# Create a new event loop that will run in a new threadnew_loop = asyncio.new_event_loop()# Start ollama serve in a separate thread so the cell won't block executionthread = threading.Thread(target=run_async_in_thread, args=(new_loop, start_ollama_serve()))thread.start()# Load up modelos.system(f"ollama pull {model_name}")import copyimport gradio as grimport spacesfrom llama_index.llms.ollama import Ollamaimport llama_indexfrom llama_index.core.llms import ChatMessageHF_TOKEN = os.environ.get("HF_TOKEN", None)MODEL_ID_LIST = ["google/gemma-2-27b-it"]MODEL_NAME = MODEL_ID.split("/")[-1]os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"gemma2 = Ollama(model=model_name, request_timeout=30.0)TITLE = "<h1><center>Chatbox</center></h1>"21 unmodified lines@spaces.GPU(duration=90)def stream_chat(message: str, history: list, temperature: float, context_window: int, top_p: float, top_k: int, penalty: float):print(f'message is - {message}')print(f'history is - {history}')conversation = []for prompt, answer in history:conversation.extend([ChatMessage(role="user", content=prompt),ChatMessage(role="assistant", content=answer),])messages = [ChatMessage(role="user", content=message)]print(f"Conversation is -\n{conversation}")resp = gemma2.stream_chat(message = messages,chat_history = conversation,top_p=top_p,top_k=top_k,repeat_penalty=penalty,context_window=context_window,)for r in resp:yield r.deltachatbot = gr.Chatbot(height=600)21 unmodified linesmaximum=2048,step=1,value=1024,label="Context window",render=False,),gr.Slider(
app.py+91-30