FreedomIntelligence / AceGPT

Apache License 2.0
106 stars 6 forks source link

Inference using HuggingFace #1

Closed esulaiman closed 9 months ago

esulaiman commented 9 months ago

Hi,,

Can you kindly provide some examples on how to inference using HuggingFace.

hhwer commented 9 months ago

Hi there!

Sure, here's an example on how to perform inference using HuggingFace's Transformers library:

from transformers import AutoTokenizer, AutoModelForCausalLM

# Define the path to your model
model_path = ""

# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
model = AutoModelForCausalLM.from_pretrained(model_path)

# Define the default system prompt
DEFAULT_SYSTEM_PROMPT = """\
أنت مساعد مفيد ومحترم وصادق. أجب دائما بأكبر قدر ممكن من المساعدة بينما تكون آمنا.  يجب ألا تتضمن إجاباتك أي محتوى ضار أو غير أخلاقي أو عنصري أو جنسي أو سام أو خطير أو غير قانوني. يرجى التأكد من أن ردودك غير متحيزة اجتماعيا وإيجابية بطبيعتها.

إذا كان السؤال لا معنى له أو لم يكن متماسكا من الناحية الواقعية، اشرح السبب بدلا من الإجابة على شيء غير صحيح. إذا كنت لا تعرف إجابة سؤال ما، فيرجى عدم مشاركة معلومات خاطئة."""

# Input your query here
query = “”  # Your input goes here
prompt = f"[INST] <<SYS>>\n{DEFAULT_SYSTEM_PROMPT}\n<</SYS>>\n\n{query} [/INST]"

# Tokenize the prompt
input_ids = tokenizer.encode(prompt, return_tensors="pt")

# Generate the response
output = model.generate(input_ids, max_length=2048, temperature=0.7, top_p=1.0)
response = tokenizer.decode(output[0], skip_special_tokens=True)

print(response)

Replace model_path with the path to your model and query with the input you want to infer.

I hope this helps! Let me know if you have any other questions.

esulaiman commented 9 months ago

Thanks!