vicky1008 commited on
Commit
f612fcc
·
verified ·
1 Parent(s): 452c1c0
Files changed (1) hide show
  1. index.html +78 -19
index.html CHANGED
@@ -1,19 +1,78 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)