Как я могу запустить функцию Python без загрузки страницы в DjangoPython

Программы на Python
Ответить
Anonymous
 Как я могу запустить функцию Python без загрузки страницы в Django

Сообщение Anonymous »

Я работаю над проектом, который содержит функцию, которая автоматически генерирует ответ в цикле с использованием Google Gemini API и отправляет его на HTML-страницу, сохраняя его в базе данных. Я хочу, чтобы эта функция выполнялась незаметно, не загружая страница.
В настоящее время происходит следующее: если функция генерирует 10 ответов в цикле, она затем продолжает загружать страницу до тех пор, пока не будут сгенерированы все ответы, а затем отображает файл home.html< /code>.
Это функция:
def home(request):
print("Bot: Hello how can i help you?")

i = 0
while i < 2:
if len(history_global) == 0:
user_input = input("You: ")
chat_session = model.start_chat(
history=history
)

response = chat_session.send_message(user_input) #1 Taking input from user

model_response = response.text

history.append({"role" : "user", "parts" : [user_input]}) #3 updating history - (user part)
history.append({"role" : "model", "parts" : [model_response]}) #4 updating history - (response part)
history_global = model_response #5 updating value of history_global with the response created

# Save the data to the database
saveit = Data1(question=user_input, answer=history_global)
saveit.save()

print(i)
print("Question : ", user_input)
print()
print("Answer : ", model_response)
print()
print("...............................................................................................1")
print()

random_interval = random.randint(10, 15)
time.sleep(random_interval)

else:
chat_session = model.start_chat(
history=history
)

new_prompt = "[make any one question from the following content] - " + history_global

response = chat_session.send_message(new_prompt) #1 Taking input from previous response
# generating output result (basically question).......
model_response = response.text #2 Generated question
history_global_question = model_response

history.append({"role" : "user", "parts" : [new_prompt]}) #3 updating history - (user part)
history.append({"role" : "model", "parts" : [model_response]}) #4 updating history - (response part)
history_global = model_response #5 updating value of history_global

random_interval_01 = random.randint(10, 15)
time.sleep(random_interval_01)

response2 = chat_session.send_message(history_global_question) #6 Taking question generated in #2 as a prompt
model_response2 = response2.text #7 Generated answer

history.append({"role" : "user", "parts" : [user_input]}) #8 updating history - (user part)
history.append({"role" : "model", "parts" : [model_response]}) #9 updating history - (response part)
history_global = model_response2 #10 updating history_global with new response

# Save the data to the database
saveit = Data1(question=history_global_question, answer=model_response2)
saveit.save()

print(i)
print("Question : ", history_global_question)
print()
print("Answer : ", model_response2)
print()
print("...............................................................................................2")
print()

random_interval = random.randint(10, 15)
time.sleep(random_interval)

i+=1

data1 = Data1.objects.all()

context = {"data_test" : data1}

return render(request, 'home.html', context)

Вот index.html
{% load static %}







Welcome




Welcome to our website

{% for data1 in data_test %}

{{data1.question_html}}

{{data1.answer_html}}
click here

{% endfor %}






Подробнее здесь: https://stackoverflow.com/questions/788 ... -in-django
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»