zalitest / app.py
Esmaeilkiani's picture
Update app.py
6e38594 verified
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, DataCollatorForSeq2Seq
from datasets import load_dataset, load_from_disk
from evaluate import load
import torch
import os
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="openaccess-ai-collective/minotaur-15b")
# Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("openaccess-ai-collective/minotaur-15b")
model = AutoModelForCausalLM.from_pretrained("openaccess-ai-collective/minotaur-15b")
model_id = "your_model_id" # Replace with your model ID
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)
def generate_answer(question, file_path):
if os.path.exists(file_path):
# Load data from file
if file_path.endswith(".csv"):
data = pd.read_csv(file_path)
elif file_path.endswith(".json"):
data = json.load(open(file_path))
else:
data = open(file_path, "r").read()
else:
data = ""
prompt = f"""
Answer the question based on the provided context:
Question: {question}
Context: {data}
Answer:
"""
inputs = tokenizer(prompt, return_tensors="pt")
input_ids = inputs.input_ids.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
attention_mask = inputs.attention_mask.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
output = model.generate(input_ids=input_ids, attention_mask=attention_mask)
answer = tokenizer.decode(output[0], skip_special_tokens=True)
return answer
def main():
question = input("Enter your question: ")
file_path = input("Enter the file path (optional): ")
answer = generate_answer(question, file_path)
print(f"Answer: {answer}")
if __name__ == "__main__":
main()