Spaces:
Build error
Build error
Tan Gezerman
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
from typing import Optional
|
|
|
|
| 2 |
import chainlit as cl
|
| 3 |
from langchain_chroma import Chroma
|
| 4 |
from langchain_core.prompts import PromptTemplate
|
|
@@ -11,20 +12,9 @@ from langchain.chains.conversation.memory import ConversationBufferMemory
|
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
-
# ctransformers is no longer used
|
| 15 |
-
""" from langchain_community.llms import CTransformers
|
| 16 |
-
|
| 17 |
-
# Initialize the language model
|
| 18 |
-
llm = CTransformers(model='Model/llama-2-7b-chat.ggmlv3.q2_K.bin', # 2 bit quantized model
|
| 19 |
-
model_type='llama',
|
| 20 |
-
config={'max_new_tokens': 256, # max tokens in reply
|
| 21 |
-
'temperature': 0.01, } # randomness of the reply
|
| 22 |
-
)
|
| 23 |
-
"""
|
| 24 |
-
|
| 25 |
# Initialize the language model with LlamaCpp
|
| 26 |
llm = LlamaCpp(model_path="Model/llama-2-7b-chat.Q4_K_M.gguf", # token streaming to terminal
|
| 27 |
-
device="cpu",verbose = True, max_tokens =
|
| 28 |
config={ # max tokens in reply
|
| 29 |
'temperature': 0.75} # randomness of the reply
|
| 30 |
)
|
|
@@ -77,7 +67,7 @@ async def on_chat_start():
|
|
| 77 |
|
| 78 |
|
| 79 |
@cl.step(type="llm")
|
| 80 |
-
def get_response(query):
|
| 81 |
"""
|
| 82 |
Generates a response from the language model based on the user's input. If the input includes
|
| 83 |
'-rag', it uses a retrieval-augmented generation pipeline, otherwise, it directly invokes
|
|
@@ -117,7 +107,7 @@ async def on_message(message: cl.Message):
|
|
| 117 |
Fetches the response from the language model and shows it in the web ui.
|
| 118 |
"""
|
| 119 |
try:
|
| 120 |
-
response = get_response(message.content)
|
| 121 |
msg = cl.Message(content=response)
|
| 122 |
except Exception as e:
|
| 123 |
msg = cl.Message(content=str(e))
|
|
@@ -138,15 +128,21 @@ def on_chat_end():
|
|
| 138 |
pass
|
| 139 |
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional, Dict
|
| 2 |
+
import os
|
| 3 |
import chainlit as cl
|
| 4 |
from langchain_chroma import Chroma
|
| 5 |
from langchain_core.prompts import PromptTemplate
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Initialize the language model with LlamaCpp
|
| 16 |
llm = LlamaCpp(model_path="Model/llama-2-7b-chat.Q4_K_M.gguf", # token streaming to terminal
|
| 17 |
+
device="cpu",verbose = True, max_tokens = 2048, #offloads ALL layers to GPU, uses around 6 GB of Vram
|
| 18 |
config={ # max tokens in reply
|
| 19 |
'temperature': 0.75} # randomness of the reply
|
| 20 |
)
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
@cl.step(type="llm")
|
| 70 |
+
async def get_response(query):
|
| 71 |
"""
|
| 72 |
Generates a response from the language model based on the user's input. If the input includes
|
| 73 |
'-rag', it uses a retrieval-augmented generation pipeline, otherwise, it directly invokes
|
|
|
|
| 107 |
Fetches the response from the language model and shows it in the web ui.
|
| 108 |
"""
|
| 109 |
try:
|
| 110 |
+
response = await get_response(message.content)
|
| 111 |
msg = cl.Message(content=response)
|
| 112 |
except Exception as e:
|
| 113 |
msg = cl.Message(content=str(e))
|
|
|
|
| 128 |
pass
|
| 129 |
|
| 130 |
|
| 131 |
+
provider_id = os.getenv('OAUTH_GOOGLE_CLIENT_ID')
|
| 132 |
+
token = os.getenv('OAUTH_GOOGLE_CLIENT_SECRET')
|
| 133 |
+
@cl.oauth_callback
|
| 134 |
+
def oauth_callback(
|
| 135 |
+
provider_id: str,
|
| 136 |
+
token: str,
|
| 137 |
+
raw_user_data: Dict[str, str],
|
| 138 |
+
default_user: cl.User,
|
| 139 |
+
) -> Optional[cl.User]:
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# Allow any Gmail user to authenticate
|
| 143 |
+
if provider_id == "google":
|
| 144 |
+
email = raw_user_data.get("email", "")
|
| 145 |
+
if email.endswith("@gmail.com"):
|
| 146 |
+
return default_user
|
| 147 |
+
|
| 148 |
+
return None
|