Я работаю над проектом, который содержит функцию, которая автоматически генерирует ответ в цикле с использованием 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
Как я могу запустить функцию Python без загрузки страницы в Django ⇐ Python
Программы на Python
-
Anonymous
1731850926
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}}
[url=contentpage/{{data1.id}}]click here[/url]
{% endfor %}
Подробнее здесь: [url]https://stackoverflow.com/questions/78883021/how-can-i-run-a-python-function-without-loading-a-page-in-django[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия