Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
from datetime import datetime
|
| 3 |
-
from huggingface_hub import hf_hub_download
|
| 4 |
from llama_cpp import Llama
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
# YOUR APP SETTINGS
|
| 8 |
APP_NAME = "ChennaiAI"
|
| 9 |
LOGO = "🤖"
|
| 10 |
|
|
@@ -18,7 +17,7 @@ print(f"Loading {APP_NAME}...")
|
|
| 18 |
llm = Llama(
|
| 19 |
model_path=MODEL_PATH,
|
| 20 |
n_ctx=4096,
|
| 21 |
-
n_threads=
|
| 22 |
n_gpu_layers=0,
|
| 23 |
verbose=False
|
| 24 |
)
|
|
@@ -26,24 +25,18 @@ llm = Llama(
|
|
| 26 |
def ai_reply(message, history):
|
| 27 |
history = history or []
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
prompt = system_prompt
|
| 32 |
-
for msg_obj in history:
|
| 33 |
-
role = msg_obj.get("role")
|
| 34 |
-
content = msg_obj.get("content", "")
|
| 35 |
-
if role == "user":
|
| 36 |
-
prompt += f"<|user|> {content} <|end|>\n"
|
| 37 |
-
elif role == "assistant":
|
| 38 |
-
prompt += f"<|assistant|> {content} <|end|>\n"
|
| 39 |
-
|
| 40 |
prompt += f"<|user|> {message} <|end|>\n<|assistant|>"
|
| 41 |
|
| 42 |
output = llm(prompt, max_tokens=512, temperature=0.7, stop=["<|user|>", "<|end|>"])
|
| 43 |
response = output['choices'][0]['text'].strip()
|
| 44 |
|
| 45 |
-
|
| 46 |
-
history.append(
|
| 47 |
return "", history
|
| 48 |
|
| 49 |
def save_chat(history):
|
|
@@ -51,16 +44,14 @@ def save_chat(history):
|
|
| 51 |
return "No chat to save"
|
| 52 |
filename = f"chat_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
|
| 53 |
with open(filename, "w", encoding="utf-8") as f:
|
| 54 |
-
for
|
| 55 |
-
|
| 56 |
-
content = msg_obj.get("content", "")
|
| 57 |
-
f.write(f"{role}: {content}\n\n")
|
| 58 |
return f"Saved as {filename}"
|
| 59 |
|
| 60 |
# YOUR APP UI
|
| 61 |
with gr.Blocks(title=APP_NAME, theme=gr.themes.Soft()) as app:
|
| 62 |
gr.Markdown(f"# {LOGO} {APP_NAME}")
|
| 63 |
-
gr.Markdown("Your own
|
| 64 |
|
| 65 |
chatbot = gr.Chatbot(height=500, label="Chat")
|
| 66 |
|
|
@@ -79,4 +70,4 @@ with gr.Blocks(title=APP_NAME, theme=gr.themes.Soft()) as app:
|
|
| 79 |
clear.click(lambda: None, None, chatbot)
|
| 80 |
save.click(save_chat, chatbot, status)
|
| 81 |
|
| 82 |
-
app.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
from datetime import datetime
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
from llama_cpp import Llama
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
| 7 |
APP_NAME = "ChennaiAI"
|
| 8 |
LOGO = "🤖"
|
| 9 |
|
|
|
|
| 17 |
llm = Llama(
|
| 18 |
model_path=MODEL_PATH,
|
| 19 |
n_ctx=4096,
|
| 20 |
+
n_threads=2, # Set to 2 vCPUs for Hugging Face free tier
|
| 21 |
n_gpu_layers=0,
|
| 22 |
verbose=False
|
| 23 |
)
|
|
|
|
| 25 |
def ai_reply(message, history):
|
| 26 |
history = history or []
|
| 27 |
|
| 28 |
+
# Format prompt from list of tuples [(user, assistant)]
|
| 29 |
+
prompt = f"<|system|>You are {APP_NAME}, a helpful AI assistant.<|end|>\n"
|
| 30 |
+
for user_msg, bot_msg in history:
|
| 31 |
+
prompt += f"<|user|> {user_msg} <|end|>\n<|assistant|> {bot_msg} <|end|>\n"
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
prompt += f"<|user|> {message} <|end|>\n<|assistant|>"
|
| 34 |
|
| 35 |
output = llm(prompt, max_tokens=512, temperature=0.7, stop=["<|user|>", "<|end|>"])
|
| 36 |
response = output['choices'][0]['text'].strip()
|
| 37 |
|
| 38 |
+
# Append as tuple format required by Gradio
|
| 39 |
+
history.append((message, response))
|
| 40 |
return "", history
|
| 41 |
|
| 42 |
def save_chat(history):
|
|
|
|
| 44 |
return "No chat to save"
|
| 45 |
filename = f"chat_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
|
| 46 |
with open(filename, "w", encoding="utf-8") as f:
|
| 47 |
+
for user_msg, bot_msg in history:
|
| 48 |
+
f.write(f"User: {user_msg}\nAssistant: {bot_msg}\n\n")
|
|
|
|
|
|
|
| 49 |
return f"Saved as {filename}"
|
| 50 |
|
| 51 |
# YOUR APP UI
|
| 52 |
with gr.Blocks(title=APP_NAME, theme=gr.themes.Soft()) as app:
|
| 53 |
gr.Markdown(f"# {LOGO} {APP_NAME}")
|
| 54 |
+
gr.Markdown("Your own AI assistant. Built with Python.")
|
| 55 |
|
| 56 |
chatbot = gr.Chatbot(height=500, label="Chat")
|
| 57 |
|
|
|
|
| 70 |
clear.click(lambda: None, None, chatbot)
|
| 71 |
save.click(save_chat, chatbot, status)
|
| 72 |
|
| 73 |
+
app.launch()
|