Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
|
| 3 |
+
tokenizer = AutoTokenizer.from_pretrained("AventIQ-AI/pythia-410m-chatbot")
|
| 4 |
+
model = AutoModelForCausalLM.from_pretrained("AventIQ-AI/pythia-410m-chatbot")
|
| 5 |
+
|
| 6 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 7 |
+
|
| 8 |
+
def chat_with_model(model, tokenizer, question, max_length=256):
|
| 9 |
+
"""Generate response to a question"""
|
| 10 |
+
input_text = question
|
| 11 |
+
|
| 12 |
+
inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 13 |
+
|
| 14 |
+
with torch.no_grad():
|
| 15 |
+
outputs = model.generate(
|
| 16 |
+
inputs["input_ids"],
|
| 17 |
+
attention_mask=inputs["attention_mask"],
|
| 18 |
+
max_length=max_length,
|
| 19 |
+
num_return_sequences=1,
|
| 20 |
+
temperature=1.0,
|
| 21 |
+
do_sample=True,
|
| 22 |
+
pad_token_id=tokenizer.pad_token_id
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 26 |
+
|
| 27 |
+
# Example usage
|
| 28 |
+
test_question = "What is the capital of France?"
|
| 29 |
+
response = chat_with_model(model, tokenizer, test_question)
|
| 30 |
+
print("Answer", response)
|