Patel Traders commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,119 @@
|
|
| 1 |
-
# --- ๐ฑ Hanuman AI:
|
| 2 |
# Pioneered by Divy Patel | Bharat ๐ฎ๐ณ
|
| 3 |
-
# Features:
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
| 7 |
-
from transformers import
|
| 8 |
-
from
|
| 9 |
import os
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
device_map="cpu",
|
| 22 |
low_cpu_mem_usage=True,
|
| 23 |
-
|
| 24 |
-
trust_remote_code=True
|
| 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 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
)[0]
|
| 86 |
-
|
| 87 |
-
history.append([user_message, response])
|
| 88 |
-
return "", history, None
|
| 89 |
-
|
| 90 |
-
except Exception as e:
|
| 91 |
-
error_msg = f"System error: {str(e)}"
|
| 92 |
-
history.append([user_message, error_msg])
|
| 93 |
-
return "", history, None
|
| 94 |
-
|
| 95 |
-
# --- UI PORTAL ---
|
| 96 |
with gr.Blocks() as demo:
|
| 97 |
-
gr.HTML("<div style='text-align: center;'><h1>๐ฑ Hanuman AI -
|
|
|
|
|
|
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
|
|
|
|
| 107 |
submit_btn.click(
|
| 108 |
-
fn=
|
| 109 |
-
inputs=[msg_input, chatbot
|
| 110 |
-
outputs=[msg_input, chatbot
|
| 111 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
-
demo.launch()
|
|
|
|
| 1 |
+
# --- ๐ฑ Hanuman AI: Google Gemma-2 Coding & Reasoning Portal ---
|
| 2 |
# Pioneered by Divy Patel | Bharat ๐ฎ๐ณ
|
| 3 |
+
# Features: Gated Access Fixed, Persistent History, 100% English
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
| 7 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 8 |
+
from threading import Thread
|
| 9 |
import os
|
| 10 |
|
| 11 |
+
# ๐ก๏ธ Accessing the Secret Token for Gated Model Access
|
| 12 |
+
# Important: Add 'HF_TOKEN' in your Hugging Face Space Settings
|
| 13 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 14 |
|
| 15 |
+
# Google's Gemma-2 2B (Optimized for Coding)
|
| 16 |
+
model_id = "google/gemma-2-2b-it"
|
| 17 |
|
| 18 |
+
print("๐ฑ Hanuman AI is awakening... Loading Google Gemma-2 with Token access.")
|
| 19 |
+
|
| 20 |
+
# Loading Tokenizer and Model with Token verification to fix 'Gated Repository' error
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token)
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
model_id,
|
| 24 |
+
torch_dtype=torch.float32,
|
| 25 |
device_map="cpu",
|
| 26 |
low_cpu_mem_usage=True,
|
| 27 |
+
token=hf_token
|
|
|
|
| 28 |
)
|
| 29 |
+
|
| 30 |
+
def hanuman_coding_engine(user_message, history):
|
| 31 |
+
"""Core logic using Google Gemma-2 for English responses with history"""
|
| 32 |
+
|
| 33 |
+
# Strictly English System Prompt
|
| 34 |
+
system_prompt = (
|
| 35 |
+
"You are 'Hanuman AI', a high-performance coding and reasoning engine pioneered by Divy Patel. "
|
| 36 |
+
"Your responses must be strictly in English. Focus on code quality and logical precision."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# ๐ ๏ธ Formatting history for Gemma-2 Template
|
| 40 |
+
full_prompt = f"<bos><start_of_turn>system\n{system_prompt}<end_of_turn>\n"
|
| 41 |
+
|
| 42 |
+
# Adding past history to the current prompt so AI remembers previous messages
|
| 43 |
+
for past_user, past_bot in history:
|
| 44 |
+
if past_user:
|
| 45 |
+
full_prompt += f"<start_of_turn>user\n{past_user}<end_of_turn>\n"
|
| 46 |
+
if past_bot:
|
| 47 |
+
full_prompt += f"<start_of_turn>model\n{past_bot}<end_of_turn>\n"
|
| 48 |
+
|
| 49 |
+
# Adding current user message
|
| 50 |
+
full_prompt += f"<start_of_turn>user\n{user_message}<end_of_turn>\n<start_of_turn>model\n"
|
| 51 |
+
|
| 52 |
+
# Tokenizing input for CPU
|
| 53 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to("cpu")
|
| 54 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
| 55 |
+
|
| 56 |
+
# Generation parameters
|
| 57 |
+
generate_kwargs = dict(
|
| 58 |
+
inputs,
|
| 59 |
+
streamer=streamer,
|
| 60 |
+
max_new_tokens=1024,
|
| 61 |
+
do_sample=False,
|
| 62 |
+
repetition_penalty=1.2
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Threading for real-time streaming response
|
| 66 |
+
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 67 |
+
thread.start()
|
| 68 |
+
|
| 69 |
+
partial_text = ""
|
| 70 |
+
for new_text in streamer:
|
| 71 |
+
partial_text += new_text
|
| 72 |
+
yield partial_text
|
| 73 |
+
|
| 74 |
+
def chat_logic(message, history):
|
| 75 |
+
"""Ensures that the history is preserved and visible to Divy Patel Ji"""
|
| 76 |
+
# Initialize the bot response in history
|
| 77 |
+
history = history or []
|
| 78 |
+
|
| 79 |
+
# Create an empty slot for the bot's response in history
|
| 80 |
+
history.append([message, ""])
|
| 81 |
+
|
| 82 |
+
# Stream the response and update history
|
| 83 |
+
for chunk in hanuman_coding_engine(message, history[:-1]):
|
| 84 |
+
history[-1][1] = chunk
|
| 85 |
+
yield "", history
|
| 86 |
+
|
| 87 |
+
# --- ๐ฑ UI PORTAL ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
+
gr.HTML("<div style='text-align: center; color: #ff5500;'><h1>๐ฑ Hanuman AI - Google Gemma 2.0</h1><p><b>Pioneered by Divy Patel | Secure Gated Access | Bharat ๐ฎ๐ณ</b></p></div>")
|
| 90 |
+
|
| 91 |
+
chatbot = gr.Chatbot(height=550, label="Chat History")
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
+
msg_input = gr.Textbox(
|
| 95 |
+
placeholder="Type your coding query and press Enter...",
|
| 96 |
+
label="Your Message",
|
| 97 |
+
scale=8
|
| 98 |
+
)
|
| 99 |
+
submit_btn = gr.Button("ASK", variant="primary", scale=2)
|
| 100 |
|
| 101 |
+
# ๐ก๏ธ Fixing History Visibility: Updating chatbot with streamed history
|
| 102 |
submit_btn.click(
|
| 103 |
+
fn=chat_logic,
|
| 104 |
+
inputs=[msg_input, chatbot],
|
| 105 |
+
outputs=[msg_input, chatbot]
|
| 106 |
)
|
| 107 |
+
|
| 108 |
+
msg_input.submit(
|
| 109 |
+
fn=chat_logic,
|
| 110 |
+
inputs=[msg_input, chatbot],
|
| 111 |
+
outputs=[msg_input, chatbot]
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
gr.Markdown("---")
|
| 115 |
+
gr.Markdown("### ๐ฑ Note for Divy Patel Ji:")
|
| 116 |
+
gr.Markdown("1. Ensure **HF_TOKEN** is added in your Space Secrets.\n2. This model runs locally on CPU - 100% Free and Private.")
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
+
demo.launch()
|