Sam-Orion
commited on
Commit
·
efbf778
1
Parent(s):
8674847
Indus 3.0 Demo
Browse files
app.py
CHANGED
|
@@ -1,78 +1,63 @@
|
|
| 1 |
-
import os
|
|
|
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
#
|
| 7 |
archive_path = hf_hub_download(
|
| 8 |
repo_id="SamOrion/Llama_3.2_3b_Hindi_Pruned",
|
| 9 |
filename="llama-3.2-3b-hindi-pruned.tar.gz",
|
| 10 |
-
repo_type="model"
|
| 11 |
)
|
|
|
|
|
|
|
| 12 |
extract_dir = "./model"
|
| 13 |
os.makedirs(extract_dir, exist_ok=True)
|
| 14 |
|
| 15 |
-
# Extract and list contents
|
| 16 |
with tarfile.open(archive_path, "r:gz") as tar:
|
| 17 |
-
tar.extractall(path=extract_dir)
|
| 18 |
-
print("Archive members:")
|
| 19 |
for member in tar.getmembers():
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
print("\nExtracted files:")
|
| 24 |
-
for root, dirs, files in os.walk(extract_dir):
|
| 25 |
-
for file in files:
|
| 26 |
-
full_path = os.path.join(root, file)
|
| 27 |
-
print(f" {full_path}")
|
| 28 |
-
|
| 29 |
-
# Check if config.json exists and show its contents
|
| 30 |
config_path = os.path.join(extract_dir, "config.json")
|
| 31 |
-
if os.path.
|
| 32 |
-
|
| 33 |
-
with open(config_path, 'r') as f:
|
| 34 |
-
config = json.load(f)
|
| 35 |
-
print(f"\nconfig.json contents: {config}")
|
| 36 |
-
else:
|
| 37 |
-
print(f"\nconfig.json not found at {config_path}")
|
| 38 |
-
|
| 39 |
-
model_path = None
|
| 40 |
-
for root, dirs, files in os.walk(extract_dir):
|
| 41 |
-
if "config.json" in files:
|
| 42 |
-
model_path = root
|
| 43 |
-
break
|
| 44 |
-
|
| 45 |
-
if model_path is None:
|
| 46 |
-
raise FileNotFoundError("config.json not found after extraction")
|
| 47 |
|
| 48 |
-
# Load
|
| 49 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 50 |
-
model
|
| 51 |
-
|
| 52 |
torch_dtype="auto",
|
| 53 |
device_map="auto",
|
| 54 |
-
low_cpu_mem_usage=True
|
| 55 |
)
|
| 56 |
|
|
|
|
| 57 |
def chat_fn(prompt, history):
|
| 58 |
history = history or []
|
| 59 |
history.append({"role": "user", "content": prompt})
|
|
|
|
| 60 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 61 |
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 62 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 63 |
history.append({"role": "assistant", "content": response})
|
| 64 |
return history, ""
|
| 65 |
|
|
|
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
-
gr.Markdown("## 🌐 Indus 3.0 Demo")
|
| 68 |
chat = gr.Chatbot(type="messages")
|
| 69 |
-
msg = gr.Textbox(placeholder="Type here
|
| 70 |
clear = gr.Button("Clear")
|
| 71 |
msg.submit(chat_fn, [msg, chat], [chat, msg])
|
| 72 |
-
clear.click(lambda:
|
| 73 |
|
|
|
|
| 74 |
if __name__ == "__main__":
|
| 75 |
-
demo.launch(
|
| 76 |
-
server_name="0.0.0.0",
|
| 77 |
-
server_port=7860
|
| 78 |
-
)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tarfile
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# 1. Download the tar.gz archive
|
| 8 |
archive_path = hf_hub_download(
|
| 9 |
repo_id="SamOrion/Llama_3.2_3b_Hindi_Pruned",
|
| 10 |
filename="llama-3.2-3b-hindi-pruned.tar.gz",
|
| 11 |
+
repo_type="model",
|
| 12 |
)
|
| 13 |
+
|
| 14 |
+
# 2. Extract into './model', stripping the top-level folder
|
| 15 |
extract_dir = "./model"
|
| 16 |
os.makedirs(extract_dir, exist_ok=True)
|
| 17 |
|
|
|
|
| 18 |
with tarfile.open(archive_path, "r:gz") as tar:
|
|
|
|
|
|
|
| 19 |
for member in tar.getmembers():
|
| 20 |
+
# Skip the first path component (the folder name)
|
| 21 |
+
parts = member.name.split("/", 1)
|
| 22 |
+
if len(parts) == 2:
|
| 23 |
+
member.name = parts[1]
|
| 24 |
+
tar.extract(member, path=extract_dir)
|
| 25 |
|
| 26 |
+
# 3. Verify that config.json is at ./model/config.json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
config_path = os.path.join(extract_dir, "config.json")
|
| 28 |
+
if not os.path.isfile(config_path):
|
| 29 |
+
raise FileNotFoundError(f"config.json not found in {extract_dir}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# 4. Load tokenizer and model straight from './model'
|
| 32 |
+
tokenizer = AutoTokenizer.from_pretrained(extract_dir)
|
| 33 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 34 |
+
extract_dir,
|
| 35 |
torch_dtype="auto",
|
| 36 |
device_map="auto",
|
| 37 |
+
low_cpu_mem_usage=True,
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# 5. Define chat function using OpenAI-style messages
|
| 41 |
def chat_fn(prompt, history):
|
| 42 |
history = history or []
|
| 43 |
history.append({"role": "user", "content": prompt})
|
| 44 |
+
|
| 45 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 46 |
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 47 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 48 |
+
|
| 49 |
history.append({"role": "assistant", "content": response})
|
| 50 |
return history, ""
|
| 51 |
|
| 52 |
+
# 6. Build Gradio interface
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
+
gr.Markdown("## 🌐 Indus 3.0 Hindi LLM Demo")
|
| 55 |
chat = gr.Chatbot(type="messages")
|
| 56 |
+
msg = gr.Textbox(placeholder="Type here…")
|
| 57 |
clear = gr.Button("Clear")
|
| 58 |
msg.submit(chat_fn, [msg, chat], [chat, msg])
|
| 59 |
+
clear.click(lambda: ([], ""), None, [chat, msg])
|
| 60 |
|
| 61 |
+
# 7. Launch without `share=True` on Spaces
|
| 62 |
if __name__ == "__main__":
|
| 63 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False, api_open=False)
|
|
|
|
|
|
|
|
|