File size: 1,549 Bytes
ff8f5d2
 
2a7a1be
ff8f5d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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()