File size: 1,409 Bytes
ea4b5e4
 
 
 
 
 
bfc6abc
9432598
 
 
 
 
c3c8551
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os

os.system('pip install duckduckgo-search -U')
os.system('pip install huggingface_hub -U')
os.system('pip install requests -U')
os.system('pip install autotrain -U')

import requests
import json
from huggingface_hub import HfFolder
from autotrain import AutoTrain

def get_answers_from_duckduckgo(query):
    url = f"https://api.duckduckgo.com/?q={query}&format=json"
    response = requests.get(url)
    data = response.json()
    
    if 'RelatedTopics' in data:
        answers = [topic['Text'] for topic in data['RelatedTopics'] if 'Text' in topic]
        return answers
    return []

def prepare_training_data(answers, query):
    training_data = []
    for answer in answers:
        training_data.append({"text": query, "label": answer})
    return training_data

def main():
    user_query = input("Type your question: ")
    answers = get_answers_from_duckduckgo(user_query)
    
    if not answers:
        print("No answers found.")
        return

    training_data = prepare_training_data(answers, user_query)

    training_file = 'training_data.json'
    with open(training_file, 'w') as f:
        json.dump(training_data, f)

    print("Training data prepared and saved!")

    auto_train = AutoTrain(dataset_path=training_file)

    auto_train.train(model_name="distilbert-base-uncased", num_train_epochs=3)

    print("Training completed!")

if __name__ == "__main__":
    main()