Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,157 +1,157 @@
|
|
| 1 |
-
import spaces
|
| 2 |
-
import json
|
| 3 |
-
import subprocess
|
| 4 |
-
from llama_cpp import Llama
|
| 5 |
-
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
| 6 |
-
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
| 7 |
-
import gradio as gr
|
| 8 |
-
from huggingface_hub import hf_hub_download
|
| 9 |
-
import logging
|
| 10 |
-
import time
|
| 11 |
-
|
| 12 |
-
logging.basicConfig(level=logging.INFO)
|
| 13 |
-
logger = logging.getLogger(__name__)
|
| 14 |
-
|
| 15 |
-
repo_id = "QuantFactory/Meta-Llama-3-8B-Instruct-GGUF"
|
| 16 |
-
filename = "Meta-Llama-3-8B-Instruct.Q8_0.gguf"
|
| 17 |
-
|
| 18 |
-
try:
|
| 19 |
-
start_time = time.time()
|
| 20 |
-
logger.info("Downloading Model....")
|
| 21 |
-
|
| 22 |
-
hf_hub_download(
|
| 23 |
-
repo_id = repo_id ,
|
| 24 |
-
filename = filename,
|
| 25 |
-
local_dir="./model"
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
end_time = time.time()
|
| 29 |
-
logger.info(f"Download complete. Time taken : {end_time - start_time} seconds.")
|
| 30 |
-
|
| 31 |
-
except Exception as e:
|
| 32 |
-
logger.error(f"Unable to download Model : {e}")
|
| 33 |
-
raise
|
| 34 |
-
|
| 35 |
-
llm = None
|
| 36 |
-
|
| 37 |
-
@spaces.GPU(duration=120)
|
| 38 |
-
def respond(message, history, max_tokens, temperature):
|
| 39 |
-
"""
|
| 40 |
-
Generate a streaming response using the llama3-8b model with chunking.
|
| 41 |
-
|
| 42 |
-
Args:
|
| 43 |
-
message (str): The input message.
|
| 44 |
-
history (list): The conversation history used by ChatInterface. - Not used.
|
| 45 |
-
temperature (float): The temperature for generating the response.
|
| 46 |
-
max_new_tokens (int): The maximum number of new tokens to generate.
|
| 47 |
-
|
| 48 |
-
Returns:
|
| 49 |
-
str: The generated response.
|
| 50 |
-
"""
|
| 51 |
-
|
| 52 |
-
chat_template = MessagesFormatterType.LLAMA_3
|
| 53 |
-
|
| 54 |
-
global llm
|
| 55 |
-
|
| 56 |
-
start_time = time.time()
|
| 57 |
-
logging.info("Loading Model...")
|
| 58 |
-
|
| 59 |
-
if llm is None:
|
| 60 |
-
model = Llama(
|
| 61 |
-
model_path=f"
|
| 62 |
-
flash_attn=True,
|
| 63 |
-
n_gpu_layers=-1,
|
| 64 |
-
n_batch=1,
|
| 65 |
-
n_ctx=8192,
|
| 66 |
-
last_n_tokens = 0
|
| 67 |
-
)
|
| 68 |
-
llm = model
|
| 69 |
-
|
| 70 |
-
end_time = time.time()
|
| 71 |
-
logger.info(f"Model Loaded. Time taken : {end_time - start_time} seconds.")
|
| 72 |
-
|
| 73 |
-
start_time = time.time()
|
| 74 |
-
logger.info("Loading Provider and Agent for the Llama Model....")
|
| 75 |
-
|
| 76 |
-
provider = LlamaCppPythonProvider(llm)
|
| 77 |
-
|
| 78 |
-
SYS_PROMPT ="""
|
| 79 |
-
Extract all relevant keywords and add quantity from the following text and format the result in nested JSON, ignoring personal details and focusing only on the scope of work as shown in the example:
|
| 80 |
-
Good JSON example: {'lobby': {'frcm': {'replace': {'carpet': 1, 'carpet_pad': 1, 'base': 1, 'window_treatments': 1, 'artwork_and_decorative_accessories': 1, 'portable_lighting': 1, 'upholstered_furniture_and_decorative_pillows': 1, 'millwork': 1} } } }
|
| 81 |
-
Bad JSON example: {'lobby': { 'frcm': { 'replace': [ 'carpet', 'carpet_pad', 'base', 'window_treatments', 'artwork_and_decorative_accessories', 'portable_lighting', 'upholstered_furniture_and_decorative_pillows', 'millwork'] } } }
|
| 82 |
-
Make sure to fetch details from the provided text and ignore unnecessary information. The response should be in JSON format only, without any additional comments.
|
| 83 |
-
"""
|
| 84 |
-
|
| 85 |
-
agent = LlamaCppAgent(
|
| 86 |
-
provider,
|
| 87 |
-
system_prompt=SYS_PROMPT,
|
| 88 |
-
predefined_messages_formatter_type=chat_template,
|
| 89 |
-
debug_output=True
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
settings = provider.get_provider_default_settings()
|
| 93 |
-
settings.temperature = temperature
|
| 94 |
-
settings.max_tokens = max_tokens
|
| 95 |
-
settings.stream = True
|
| 96 |
-
|
| 97 |
-
end_time = time.time()
|
| 98 |
-
logger.info(f"Provider settings updated. Prompt Loaded.Time taken : {end_time - start_time} seconds.")
|
| 99 |
-
|
| 100 |
-
response = agent.get_chat_response(
|
| 101 |
-
message,
|
| 102 |
-
llm_sampling_settings=settings,
|
| 103 |
-
returns_streaming_generator=False,
|
| 104 |
-
print_output=False
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
-
return response
|
| 108 |
-
|
| 109 |
-
DESCRIPTION = '''
|
| 110 |
-
<div>
|
| 111 |
-
<h1 style="text-align: center;">ContenteaseAI custom trained model</h1>
|
| 112 |
-
</div>
|
| 113 |
-
'''
|
| 114 |
-
|
| 115 |
-
LICENSE = """
|
| 116 |
-
<p/>
|
| 117 |
-
---
|
| 118 |
-
For more information, visit our [website](https://contentease.ai).
|
| 119 |
-
"""
|
| 120 |
-
|
| 121 |
-
PLACEHOLDER = """
|
| 122 |
-
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
|
| 123 |
-
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">ContenteaseAI Custom AI trained model</h1>
|
| 124 |
-
<p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Enter the text extracted from the PDF:</p>
|
| 125 |
-
</div>
|
| 126 |
-
"""
|
| 127 |
-
|
| 128 |
-
css = """
|
| 129 |
-
h1 {
|
| 130 |
-
text-align: center;
|
| 131 |
-
display: block;
|
| 132 |
-
}
|
| 133 |
-
"""
|
| 134 |
-
# Gradio block
|
| 135 |
-
chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
| 136 |
-
|
| 137 |
-
with gr.Blocks(fill_height=True, css=css) as demo:
|
| 138 |
-
gr.Markdown(DESCRIPTION)
|
| 139 |
-
|
| 140 |
-
gr.ChatInterface(
|
| 141 |
-
fn=respond,
|
| 142 |
-
chatbot=chatbot,
|
| 143 |
-
fill_height=True,
|
| 144 |
-
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
| 145 |
-
additional_inputs=[
|
| 146 |
-
gr.Slider(minimum=0, maximum=1, step=0.1, value=0.90, label="Temperature", render=False),
|
| 147 |
-
gr.Slider(minimum=128, maximum=2000, step=1, value=1500, label="Max new tokens", render=False),
|
| 148 |
-
]
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
gr.Markdown(LICENSE)
|
| 152 |
-
|
| 153 |
-
if __name__ == "__main__":
|
| 154 |
-
try:
|
| 155 |
-
demo.launch(show_error=True, debug = True)
|
| 156 |
-
except Exception as e:
|
| 157 |
logger.error(f"Error launching Gradio demo: {e}")
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import json
|
| 3 |
+
import subprocess
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
+
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
| 6 |
+
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
import logging
|
| 10 |
+
import time
|
| 11 |
+
|
| 12 |
+
logging.basicConfig(level=logging.INFO)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
repo_id = "QuantFactory/Meta-Llama-3-8B-Instruct-GGUF"
|
| 16 |
+
filename = "Meta-Llama-3-8B-Instruct.Q8_0.gguf"
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
start_time = time.time()
|
| 20 |
+
logger.info("Downloading Model....")
|
| 21 |
+
|
| 22 |
+
hf_hub_download(
|
| 23 |
+
repo_id = repo_id ,
|
| 24 |
+
filename = filename,
|
| 25 |
+
local_dir="./model"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
end_time = time.time()
|
| 29 |
+
logger.info(f"Download complete. Time taken : {end_time - start_time} seconds.")
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
logger.error(f"Unable to download Model : {e}")
|
| 33 |
+
raise
|
| 34 |
+
|
| 35 |
+
llm = None
|
| 36 |
+
|
| 37 |
+
@spaces.GPU(duration=120)
|
| 38 |
+
def respond(message, history, max_tokens, temperature):
|
| 39 |
+
"""
|
| 40 |
+
Generate a streaming response using the llama3-8b model with chunking.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
message (str): The input message.
|
| 44 |
+
history (list): The conversation history used by ChatInterface. - Not used.
|
| 45 |
+
temperature (float): The temperature for generating the response.
|
| 46 |
+
max_new_tokens (int): The maximum number of new tokens to generate.
|
| 47 |
+
|
| 48 |
+
Returns:
|
| 49 |
+
str: The generated response.
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
chat_template = MessagesFormatterType.LLAMA_3
|
| 53 |
+
|
| 54 |
+
global llm
|
| 55 |
+
|
| 56 |
+
start_time = time.time()
|
| 57 |
+
logging.info("Loading Model...")
|
| 58 |
+
|
| 59 |
+
if llm is None:
|
| 60 |
+
model = Llama(
|
| 61 |
+
model_path=f"model/{filename}",
|
| 62 |
+
flash_attn=True,
|
| 63 |
+
n_gpu_layers=-1,
|
| 64 |
+
n_batch=1,
|
| 65 |
+
n_ctx=8192,
|
| 66 |
+
last_n_tokens = 0
|
| 67 |
+
)
|
| 68 |
+
llm = model
|
| 69 |
+
|
| 70 |
+
end_time = time.time()
|
| 71 |
+
logger.info(f"Model Loaded. Time taken : {end_time - start_time} seconds.")
|
| 72 |
+
|
| 73 |
+
start_time = time.time()
|
| 74 |
+
logger.info("Loading Provider and Agent for the Llama Model....")
|
| 75 |
+
|
| 76 |
+
provider = LlamaCppPythonProvider(llm)
|
| 77 |
+
|
| 78 |
+
SYS_PROMPT ="""
|
| 79 |
+
Extract all relevant keywords and add quantity from the following text and format the result in nested JSON, ignoring personal details and focusing only on the scope of work as shown in the example:
|
| 80 |
+
Good JSON example: {'lobby': {'frcm': {'replace': {'carpet': 1, 'carpet_pad': 1, 'base': 1, 'window_treatments': 1, 'artwork_and_decorative_accessories': 1, 'portable_lighting': 1, 'upholstered_furniture_and_decorative_pillows': 1, 'millwork': 1} } } }
|
| 81 |
+
Bad JSON example: {'lobby': { 'frcm': { 'replace': [ 'carpet', 'carpet_pad', 'base', 'window_treatments', 'artwork_and_decorative_accessories', 'portable_lighting', 'upholstered_furniture_and_decorative_pillows', 'millwork'] } } }
|
| 82 |
+
Make sure to fetch details from the provided text and ignore unnecessary information. The response should be in JSON format only, without any additional comments.
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
agent = LlamaCppAgent(
|
| 86 |
+
provider,
|
| 87 |
+
system_prompt=SYS_PROMPT,
|
| 88 |
+
predefined_messages_formatter_type=chat_template,
|
| 89 |
+
debug_output=True
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
settings = provider.get_provider_default_settings()
|
| 93 |
+
settings.temperature = temperature
|
| 94 |
+
settings.max_tokens = max_tokens
|
| 95 |
+
settings.stream = True
|
| 96 |
+
|
| 97 |
+
end_time = time.time()
|
| 98 |
+
logger.info(f"Provider settings updated. Prompt Loaded.Time taken : {end_time - start_time} seconds.")
|
| 99 |
+
|
| 100 |
+
response = agent.get_chat_response(
|
| 101 |
+
message,
|
| 102 |
+
llm_sampling_settings=settings,
|
| 103 |
+
returns_streaming_generator=False,
|
| 104 |
+
print_output=False
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
return response
|
| 108 |
+
|
| 109 |
+
DESCRIPTION = '''
|
| 110 |
+
<div>
|
| 111 |
+
<h1 style="text-align: center;">ContenteaseAI custom trained model</h1>
|
| 112 |
+
</div>
|
| 113 |
+
'''
|
| 114 |
+
|
| 115 |
+
LICENSE = """
|
| 116 |
+
<p/>
|
| 117 |
+
---
|
| 118 |
+
For more information, visit our [website](https://contentease.ai).
|
| 119 |
+
"""
|
| 120 |
+
|
| 121 |
+
PLACEHOLDER = """
|
| 122 |
+
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
|
| 123 |
+
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">ContenteaseAI Custom AI trained model</h1>
|
| 124 |
+
<p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Enter the text extracted from the PDF:</p>
|
| 125 |
+
</div>
|
| 126 |
+
"""
|
| 127 |
+
|
| 128 |
+
css = """
|
| 129 |
+
h1 {
|
| 130 |
+
text-align: center;
|
| 131 |
+
display: block;
|
| 132 |
+
}
|
| 133 |
+
"""
|
| 134 |
+
# Gradio block
|
| 135 |
+
chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
| 136 |
+
|
| 137 |
+
with gr.Blocks(fill_height=True, css=css) as demo:
|
| 138 |
+
gr.Markdown(DESCRIPTION)
|
| 139 |
+
|
| 140 |
+
gr.ChatInterface(
|
| 141 |
+
fn=respond,
|
| 142 |
+
chatbot=chatbot,
|
| 143 |
+
fill_height=True,
|
| 144 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
| 145 |
+
additional_inputs=[
|
| 146 |
+
gr.Slider(minimum=0, maximum=1, step=0.1, value=0.90, label="Temperature", render=False),
|
| 147 |
+
gr.Slider(minimum=128, maximum=2000, step=1, value=1500, label="Max new tokens", render=False),
|
| 148 |
+
]
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
gr.Markdown(LICENSE)
|
| 152 |
+
|
| 153 |
+
if __name__ == "__main__":
|
| 154 |
+
try:
|
| 155 |
+
demo.launch(show_error=True, debug = True)
|
| 156 |
+
except Exception as e:
|
| 157 |
logger.error(f"Error launching Gradio demo: {e}")
|