DiYane

work

May 10th, 2023
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. from flask import Flask, request, render_template_string
  2. import query_chatbot
  3.  
  4. app = Flask(__name__)
  5.  
  6. @app.route('/', methods=['GET', 'POST'])
  7. def index_input():
  8.     if request.method == 'POST':
  9.         author_name = request.form['author_name']
  10.         book_format = request.form['book_format']
  11.         book_style = request.form['book_style']
  12.         background_info = request.form['background_info']
  13.         index_content = request.form['index_content']
  14.  
  15.         user_personality = f"I am a chatbot that was designed by {author_name} to write a book about quantum hypnosis. The book is written in a {book_style} style and is available in {book_format}."
  16.         additional_context = f"{background_info} The book is organized as follows:"
  17.  
  18.         organized_titles = query_chatbot.generate_titles(index_content)
  19.         collected_texts = []
  20.  
  21.         api_url = "http://localhost:5001/api/chatbot"
  22.  
  23.         for title in organized_titles:
  24.             text = query_chatbot.query_chatbot(api_url, title, user_personality, additional_context)
  25.             collected_texts.append(text)
  26.  
  27.         with open('collected_texts.txt', 'w') as output_file:
  28.             for text in collected_texts:
  29.                 output_file.write(text + '\n\n')
  30.  
  31.         return 'Book content has been generated and saved to collected_texts.txt'
  32.     else:
  33.         form_html = '''
  34. <!DOCTYPE html>
  35. <html>
  36. <head>
  37. <title>Index Input</title>
  38. </head>
  39. <body>
  40.    <h1>Enter book index and additional details</h1>
  41.    <form action="/" method="post">
  42.        <label for="author_name">Author Name:</label>
  43.        <input type="text" id="author_name" name="author_name" required><br><br>
  44.        <label for="book_format">Book Format:</label>
  45.        <input type="text" id="book_format" name="book_format" required><br><br>
  46.        <label for="book_style">Book Style:</label>
  47.        <input type="text" id="book_style" name="book_style" required><br><br>
  48.        <label for="background_info">Background Information:</label>
  49.        <textarea id="background_info" name="background_info" rows="4" cols="50" required></textarea><br><br>
  50.        <label for="index_content">Book Index:</label>
  51.        <textarea id="index_content" name="index_content" rows="20" cols="50" required></textarea><br><br>
  52.        <input type="submit" value="Submit">
  53.    </form>
  54. </body>
  55. </html>
  56. '''
  57.         return render_template_string(form_html)
  58.  
  59. if __name__ == '__main__':
  60.     app.run(host="127.0.0.1", port=5004)
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment