MyChat / main.py
Hackavist's picture
Update main.py
2a7a1be verified
raw
history blame
1.55 kB
from transformers import DistilBertTokenizerFast, DistilBertForQuestionAnswering
import json
import streamlit as st
model_name = "distilbert-base-cased"
tokenizer = DistilBertTokenizerFast.from_pretrained(model_name)
model = DistilBertForQuestionAnswering.from_pretrained(model_name)
def format_response(start_index, end_index, raw_answer):
answer_tokens = tokenizer.convert_tokens_to_string([tokenizer.convert_ids_to_tokens(i)[0] for i in range(start_index, end_index+1)])
return {'answer': answer_tokens.strip(), 'score': None}
def get_answers(question, context):
inputs = tokenizer.encode_plus(question, context, return_tensors="pt")
start_scores, end_scores = model(**inputs).values()
start_index = torch.argmax(start_scores)
end_index = torch.argmax(end_scores) + 1
formatted_answer = format_response(start_index, end_index - 1, context[start_index:end_index].tolist())
return formatted_answer
def interactive():
print("Hi! I am a simple AI chatbot built using Hugging Face.")
while True:
query = input("\nAsk me something or type 'quit' to exit:\n").lower().strip()
if query == "quit":
break
try:
# Add some basic context here; replace with your own dataset later
context = "The capital of France is Paris."
response = get_answers(query, context)
print(f"\n{json.dumps(response)}")
except Exception as e:
print(f"Error occurred: {str(e)}")
if __name__ == "__main__":
interactive()