File size: 2,284 Bytes
f612fcc fc87998 b7af75e f612fcc b7af75e f612fcc 37e66f6 f612fcc b7af75e f612fcc fc87998 b7af75e f612fcc b7af75e fc87998 b7af75e f612fcc b7af75e fc87998 f612fcc b7af75e f612fcc fc87998 f612fcc b7af75e f612fcc fc87998 b7af75e f612fcc b7af75e f612fcc b7af75e f612fcc b7af75e f612fcc b7af75e fc87998 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import os
from datetime import datetime
from huggingface_hub import hf_hub_download
from llama_cpp import Llama
import gradio as gr
APP_NAME = "ChennaiAI"
LOGO = "🤖"
MODEL_PATH = hf_hub_download(
repo_id="microsoft/Phi-3-mini-4k-instruct-gguf",
filename="Phi-3-mini-4k-instruct-q4.gguf"
)
# LOAD MODEL
print(f"Loading {APP_NAME}...")
llm = Llama(
model_path=MODEL_PATH,
n_ctx=4096,
n_threads=2, # Set to 2 vCPUs for Hugging Face free tier
n_gpu_layers=0,
verbose=False
)
def ai_reply(message, history):
history = history or []
# Format prompt from list of tuples [(user, assistant)]
prompt = f"<|system|>You are {APP_NAME}, a helpful AI assistant.<|end|>\n"
for user_msg, bot_msg in history:
prompt += f"<|user|> {user_msg} <|end|>\n<|assistant|> {bot_msg} <|end|>\n"
prompt += f"<|user|> {message} <|end|>\n<|assistant|>"
output = llm(prompt, max_tokens=512, temperature=0.7, stop=["<|user|>", "<|end|>"])
response = output['choices'][0]['text'].strip()
# Append as tuple format required by Gradio
history.append((message, response))
return "", history
def save_chat(history):
if not history:
return "No chat to save"
filename = f"chat_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
with open(filename, "w", encoding="utf-8") as f:
for user_msg, bot_msg in history:
f.write(f"User: {user_msg}\nAssistant: {bot_msg}\n\n")
return f"Saved as {filename}"
# YOUR APP UI
with gr.Blocks(title=APP_NAME, theme=gr.themes.Soft()) as app:
gr.Markdown(f"# {LOGO} {APP_NAME}")
gr.Markdown("Your own AI assistant. Built with Python.")
chatbot = gr.Chatbot(height=500, label="Chat")
with gr.Row():
msg = gr.Textbox(label="Ask me anything", placeholder="Type here...", scale=4)
send = gr.Button("Send", scale=1)
with gr.Row():
clear = gr.Button("Clear Chat")
save = gr.Button("Save Chat")
status = gr.Textbox(label="Status", interactive=False)
send.click(ai_reply, [msg, chatbot], [msg, chatbot])
msg.submit(ai_reply, [msg, chatbot], [msg, chatbot])
clear.click(lambda: None, None, chatbot)
save.click(save_chat, chatbot, status)
app.launch() |