|
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() |