Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,53 @@
|
|
| 1 |
-
from llama_cpp import Llama
|
| 2 |
-
import gradio as gr
|
| 3 |
import os
|
| 4 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# YOUR APP SETTINGS
|
| 8 |
-
APP_NAME = "ChennaiAI" # Change this to your app name
|
| 9 |
-
LOGO = "🤖" # Change emoji
|
| 10 |
MODEL_PATH = hf_hub_download(
|
| 11 |
repo_id="microsoft/Phi-3-mini-4k-instruct-gguf",
|
| 12 |
filename="Phi-3-mini-4k-instruct-q4.gguf"
|
| 13 |
)
|
| 14 |
|
| 15 |
-
# LOAD MODEL
|
| 16 |
print(f"Loading {APP_NAME}...")
|
| 17 |
llm = Llama(
|
| 18 |
model_path=MODEL_PATH,
|
| 19 |
n_ctx=4096,
|
| 20 |
n_threads=4,
|
| 21 |
-
n_gpu_layers=0,
|
| 22 |
verbose=False
|
| 23 |
)
|
| 24 |
|
| 25 |
def ai_reply(message, history):
|
| 26 |
history = history or []
|
| 27 |
-
|
| 28 |
-
# System prompt = This makes your app behave differently
|
| 29 |
system_prompt = f"<|system|>You are {APP_NAME}, a helpful AI assistant made in India.<|end|>\n"
|
| 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 |
history.append({"role": "user", "content": message})
|
| 46 |
history.append({"role": "assistant", "content": response})
|
| 47 |
return "", history
|
| 48 |
|
| 49 |
def save_chat(history):
|
| 50 |
-
if not 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:
|
|
@@ -57,25 +57,26 @@ def save_chat(history):
|
|
| 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 offline AI assistant. Built with Python.")
|
| 64 |
-
|
| 65 |
chatbot = gr.Chatbot(height=500, label="Chat")
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
msg = gr.Textbox(label="Ask me anything", placeholder="Type here...", scale=4)
|
| 69 |
send = gr.Button("Send", scale=1)
|
| 70 |
-
|
| 71 |
with gr.Row():
|
| 72 |
clear = gr.Button("Clear Chat")
|
| 73 |
save = gr.Button("Save Chat")
|
| 74 |
-
|
| 75 |
status = gr.Textbox(label="Status", interactive=False)
|
| 76 |
-
|
| 77 |
send.click(ai_reply, [msg, chatbot], [msg, chatbot])
|
| 78 |
msg.submit(ai_reply, [msg, chatbot], [msg, chatbot])
|
| 79 |
clear.click(lambda: None, None, chatbot)
|
| 80 |
save.click(save_chat, chatbot, status)
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from datetime import datetime
|
| 3 |
+
from huggingface_hub import hf_hub_download # Added missing import
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
+
import gradio as gr
|
| 6 |
|
| 7 |
+
# YOUR APP SETTINGS
|
| 8 |
+
APP_NAME = "ChennaiAI"
|
| 9 |
+
LOGO = "🤖"
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
MODEL_PATH = hf_hub_download(
|
| 12 |
repo_id="microsoft/Phi-3-mini-4k-instruct-gguf",
|
| 13 |
filename="Phi-3-mini-4k-instruct-q4.gguf"
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# LOAD MODEL
|
| 17 |
print(f"Loading {APP_NAME}...")
|
| 18 |
llm = Llama(
|
| 19 |
model_path=MODEL_PATH,
|
| 20 |
n_ctx=4096,
|
| 21 |
n_threads=4,
|
| 22 |
+
n_gpu_layers=0,
|
| 23 |
verbose=False
|
| 24 |
)
|
| 25 |
|
| 26 |
def ai_reply(message, history):
|
| 27 |
history = history or []
|
| 28 |
+
|
|
|
|
| 29 |
system_prompt = f"<|system|>You are {APP_NAME}, a helpful AI assistant made in India.<|end|>\n"
|
| 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 |
history.append({"role": "user", "content": message})
|
| 46 |
history.append({"role": "assistant", "content": response})
|
| 47 |
return "", history
|
| 48 |
|
| 49 |
def save_chat(history):
|
| 50 |
+
if not 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:
|
|
|
|
| 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 offline AI assistant. Built with Python.")
|
| 64 |
+
|
| 65 |
chatbot = gr.Chatbot(height=500, label="Chat")
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
msg = gr.Textbox(label="Ask me anything", placeholder="Type here...", scale=4)
|
| 69 |
send = gr.Button("Send", scale=1)
|
| 70 |
+
|
| 71 |
with gr.Row():
|
| 72 |
clear = gr.Button("Clear Chat")
|
| 73 |
save = gr.Button("Save Chat")
|
| 74 |
+
|
| 75 |
status = gr.Textbox(label="Status", interactive=False)
|
| 76 |
+
|
| 77 |
send.click(ai_reply, [msg, chatbot], [msg, chatbot])
|
| 78 |
msg.submit(ai_reply, [msg, chatbot], [msg, chatbot])
|
| 79 |
clear.click(lambda: None, None, chatbot)
|
| 80 |
save.click(save_chat, chatbot, status)
|
| 81 |
+
|
| 82 |
+
app.launch() # Removed share=True
|