project
Browse files- index.html +78 -19
index.html
CHANGED
|
@@ -1,19 +1,78 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
MODEL_PATH = r"C:\Users\Elakeya\OneDrive\Desktop\Phi-3-mini-4k-instruct-q4.gguf"
|
| 10 |
+
LOGO = "🤖" # Change emoji
|
| 11 |
+
|
| 12 |
+
# LOAD MODEL
|
| 13 |
+
print(f"Loading {APP_NAME}...")
|
| 14 |
+
llm = Llama(
|
| 15 |
+
model_path=MODEL_PATH,
|
| 16 |
+
n_ctx=4096,
|
| 17 |
+
n_threads=8,
|
| 18 |
+
n_gpu_layers=0, # Set 35 for Nvidia GPU
|
| 19 |
+
verbose=False
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
def ai_reply(message, history):
|
| 23 |
+
history = history or []
|
| 24 |
+
|
| 25 |
+
# System prompt = This makes your app behave differently
|
| 26 |
+
system_prompt = f"<|system|>You are {APP_NAME}, a helpful AI assistant made in India.<|end|>\n"
|
| 27 |
+
|
| 28 |
+
prompt = system_prompt
|
| 29 |
+
for msg_obj in history:
|
| 30 |
+
role = msg_obj.get("role")
|
| 31 |
+
content = msg_obj.get("content", "")
|
| 32 |
+
if role == "user":
|
| 33 |
+
prompt += f"<|user|>{content}<|end|>\n"
|
| 34 |
+
elif role == "assistant":
|
| 35 |
+
prompt += f"<|assistant|>{content}<|end|>\n"
|
| 36 |
+
|
| 37 |
+
prompt += f"<|user|>{message}<|end|>\n<|assistant|>"
|
| 38 |
+
|
| 39 |
+
output = llm(prompt, max_tokens=512, temperature=0.7, stop=["<|user|>", "<|end|>"])
|
| 40 |
+
response = output['choices'][0]['text'].strip()
|
| 41 |
+
|
| 42 |
+
history.append({"role": "user", "content": message})
|
| 43 |
+
history.append({"role": "assistant", "content": response})
|
| 44 |
+
return "", history
|
| 45 |
+
|
| 46 |
+
def save_chat(history):
|
| 47 |
+
if not history:
|
| 48 |
+
return "No chat to save"
|
| 49 |
+
filename = f"chat_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
|
| 50 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 51 |
+
for msg_obj in history:
|
| 52 |
+
role = msg_obj.get("role", "unknown").capitalize()
|
| 53 |
+
content = msg_obj.get("content", "")
|
| 54 |
+
f.write(f"{role}: {content}\n\n")
|
| 55 |
+
return f"Saved as {filename}"
|
| 56 |
+
|
| 57 |
+
# YOUR APP UI (Gradio 6.0 compatible)
|
| 58 |
+
with gr.Blocks(title=APP_NAME, theme=gr.themes.Soft()) as app:
|
| 59 |
+
gr.Markdown(f"# {LOGO} {APP_NAME}")
|
| 60 |
+
gr.Markdown("Your own offline AI assistant. Built with Python.")
|
| 61 |
+
|
| 62 |
+
chatbot = gr.Chatbot(height=500, label="Chat")
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
msg = gr.Textbox(label="Ask me anything", placeholder="Type here...", scale=4)
|
| 66 |
+
send = gr.Button("Send", scale=1)
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
clear = gr.Button("Clear Chat")
|
| 70 |
+
save = gr.Button("Save Chat")
|
| 71 |
+
|
| 72 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 73 |
+
|
| 74 |
+
send.click(ai_reply, [msg, chatbot], [msg, chatbot])
|
| 75 |
+
msg.submit(ai_reply, [msg, chatbot], [msg, chatbot])
|
| 76 |
+
clear.click(lambda: None, None, chatbot)
|
| 77 |
+
save.click(save_chat, chatbot, status)
|
| 78 |
+
app.launch(share=True)
|