derricka59 commited on
Commit
1e2cce7
·
verified ·
1 Parent(s): 0d07e61

Create app.py

Browse files

from huggingface_hub import HfApi, HfFolder
from pathlib import Path

# --- Configuration ---
# Your Hugging Face username
hf_username = "your-hf-username"
# The name of the new repository
repo_name = "deepseek-chatbot"
# The local path to your app.py file
local_app_py_path = "app.py"
# The path where the file should be in the repository
repo_app_py_path = "app.py"
# Your Hugging Face API token (replace with your actual token)
hf_token = "your-hf-token"

# --- Script ---
api = HfApi()
repo_id = f"{hf_username}/{repo_name}"

# Create the repository if it doesn't exist
try:
api.create_repo(repo_id=repo_id, token=hf_token, repo_type="space", space_sdk="docker")
print(f"Repository '{repo_id}' created on Hugging Face Hub.")
except Exception as e:
print(f"Repository '{repo_id}' may already exist: {e}")

# Read the content of the app.py file
app_py_content = Path(local_app_py_path).read_text()

# Commit the app.py file to the repository
try:
api.upload_file(
path_or_fileobj=app_py_content.encode("utf-8"),
path_in_repo=repo_app_py_path,
repo_id=repo_id,
token=hf_token,
commit_message="Add app.py for DeepSeek chatbot",
)
print(f"File '{repo_app_py_path}' committed to repository '{repo_id}'.")
except Exception as e:
print(f"Error committing file: {e}")

Files changed (1) hide show
  1. app.py +13 -0
app.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForCausalLM
2
+ import torch
3
+
4
+ tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/deepseek-coder-33b-instruct", trust_remote_code=True)
5
+ model = AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-coder-33b-instruct", trust_remote_code=True, torch_dtype=torch.bfloat16).cuda()
6
+
7
+ messages=[
8
+ { 'role': 'user', 'content': "who are you?"}
9
+ ]
10
+ inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
11
+ # tokenizer.eos_token_id is the id of <|EOT|> token
12
+ outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, top_k=50, top_p=0.95, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
13
+ print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))