Illia56 commited on
Commit
6517701
Β·
1 Parent(s): eff8a2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -50
app.py CHANGED
@@ -6,22 +6,11 @@ import markdown
6
  from bardapi import Bard
7
  from telegraph import Telegraph
8
  import time
 
9
  # Set up the Telegraph client
10
  telegraph = Telegraph()
11
-
12
  telegraph.create_account(short_name='BookMindAI')
13
 
14
- token = os.getenv("BARD_API_TOKEN")
15
- bard = Bard(token=token)
16
-
17
-
18
-
19
- # Load detail_queries from JSON
20
- with open('detail_queries.json', 'r') as file:
21
- detail_queries = json.load(file)
22
- with open('lang.json', 'r') as file:
23
- languages = [str(x) for x in json.load(file).keys()]
24
-
25
  def markdown_to_html(md_content):
26
  return markdown.markdown(md_content)
27
 
@@ -48,15 +37,13 @@ def post_to_telegraph(title, content):
48
  )
49
  return 'https://telegra.ph/{}'.format(response['path'])
50
 
51
-
52
-
53
- def generate_predictions(book_name, author, language_choice, detail_options=[]):
54
- bard = Bard(token=token, language=language_choice[3:].lower())
55
- image_links = fetch_book_cover(bard,book_name, author, language_choice)
56
 
57
  details = ""
58
  for option in detail_options:
59
- query_template = detail_queries.get(option).format(book_name=book_name, author=author)# + ' Rule 1: Output answer in in {language_choice} language.'
60
  try:
61
  response = bard.get_answer(query_template)
62
  details += f"\n\n**{option}**:\n{response['content']}"
@@ -68,15 +55,13 @@ def generate_predictions(book_name, author, language_choice, detail_options=[]):
68
  except:
69
  pass
70
 
71
-
72
- summary = fetch_summary(bard,book_name, author)
73
  combined_summary = summary["content"] + details
74
  try:
75
  telegraph_url = post_to_telegraph(f"Summary of {book_name} by {author}", combined_summary)
76
  except requests.exceptions.ConnectionError:
77
  telegraph_url = "Error connecting to Telegraph API"
78
 
79
-
80
  return image_links, combined_summary, telegraph_url
81
 
82
  with gr.Blocks(title="πŸ“š BookMindAI", theme=gr.themes.Base()).queue() as demo:
@@ -84,52 +69,31 @@ with gr.Blocks(title="πŸ“š BookMindAI", theme=gr.themes.Base()).queue() as demo:
84
  with gr.Tab("Summarize book🎯"):
85
  with gr.Row():
86
  with gr.Column():
 
87
  book_name_input = gr.Textbox(placeholder="Enter Book Name", label="Book Name")
88
  author_name_input = gr.Textbox(placeholder="Enter Author Name", label="Author Name")
89
  language_input = gr.Dropdown(choices=languages, label="Language")
90
  detail_options_input = gr.CheckboxGroup(choices=list(detail_queries.keys()), label="Details to Include", visible=True)
91
  run_button = gr.Button(label="Run", visible=True)
92
 
93
-
94
  with gr.Column():
95
  book_cover_output = gr.Gallery(label="Book Cover", visible=True)
96
  telegraph_link_output = gr.Markdown(label="View on Telegraph", visible=True)
97
  with gr.Row():
98
  summary_output = gr.Markdown(label="Parsed Content", visible=True)
99
-
100
-
101
 
102
  run_button.click(fn=generate_predictions,
103
- inputs=[book_name_input, author_name_input, language_input, detail_options_input],
104
  outputs=[book_cover_output, summary_output, telegraph_link_output],
105
  show_progress=True, queue=True)
106
 
107
-
108
-
109
-
110
- # Adding examples to the interface
111
  examples = [
112
- ["Harry Potter and the Philosopher's Stone", "J.K. Rowling", "πŸ‡¬πŸ‡§ english"],
113
- ["Pride and Prejudice", "Jane Austen", "πŸ‡ΊπŸ‡¦ ukrainian"],
114
- ["The Great Gatsby", "F. Scott Fitzgerald", "πŸ‡«πŸ‡· french"]
115
  ]
116
- gr.Examples(examples=examples, inputs=[book_name_input, author_name_input, language_input, detail_options_input])
117
  with gr.Tab("Talk about bookπŸŽ“"):
118
-
119
- def chat_response(message, history):
120
- for i in range(len(message)):
121
- response = bard.get_answer(message)
122
- yield response['content']
123
-
124
- examples = [
125
- "How do the underlying themes of a book reflect the societal values and beliefs of its time?",
126
- "In what ways do the characters' personal journeys mirror the broader human experience?",
127
- "How does the author's use of symbolism and allegory provide insight into the deeper truths of our existence?",
128
- "To what extent does the narrative structure of the book challenge or reinforce our understanding of reality?"
129
- ]
130
-
131
- chat_interface = gr.ChatInterface(chat_response, examples=examples, title ='Talk with Palm 2 about any book.')
132
-
133
-
134
  demo.launch(share=False)
135
-
 
6
  from bardapi import Bard
7
  from telegraph import Telegraph
8
  import time
9
+
10
  # Set up the Telegraph client
11
  telegraph = Telegraph()
 
12
  telegraph.create_account(short_name='BookMindAI')
13
 
 
 
 
 
 
 
 
 
 
 
 
14
  def markdown_to_html(md_content):
15
  return markdown.markdown(md_content)
16
 
 
37
  )
38
  return 'https://telegra.ph/{}'.format(response['path'])
39
 
40
+ def generate_predictions(api_token, book_name, author, language_choice, detail_options=[]):
41
+ bard = Bard(token=api_token, language=language_choice[3:].lower())
42
+ image_links = fetch_book_cover(bard, book_name, author, language_choice)
 
 
43
 
44
  details = ""
45
  for option in detail_options:
46
+ query_template = detail_queries.get(option).format(book_name=book_name, author=author)
47
  try:
48
  response = bard.get_answer(query_template)
49
  details += f"\n\n**{option}**:\n{response['content']}"
 
55
  except:
56
  pass
57
 
58
+ summary = fetch_summary(bard, book_name, author)
 
59
  combined_summary = summary["content"] + details
60
  try:
61
  telegraph_url = post_to_telegraph(f"Summary of {book_name} by {author}", combined_summary)
62
  except requests.exceptions.ConnectionError:
63
  telegraph_url = "Error connecting to Telegraph API"
64
 
 
65
  return image_links, combined_summary, telegraph_url
66
 
67
  with gr.Blocks(title="πŸ“š BookMindAI", theme=gr.themes.Base()).queue() as demo:
 
69
  with gr.Tab("Summarize book🎯"):
70
  with gr.Row():
71
  with gr.Column():
72
+ api_token_input = gr.Textbox(placeholder="Enter Bard API Token", label="Bard API Token")
73
  book_name_input = gr.Textbox(placeholder="Enter Book Name", label="Book Name")
74
  author_name_input = gr.Textbox(placeholder="Enter Author Name", label="Author Name")
75
  language_input = gr.Dropdown(choices=languages, label="Language")
76
  detail_options_input = gr.CheckboxGroup(choices=list(detail_queries.keys()), label="Details to Include", visible=True)
77
  run_button = gr.Button(label="Run", visible=True)
78
 
 
79
  with gr.Column():
80
  book_cover_output = gr.Gallery(label="Book Cover", visible=True)
81
  telegraph_link_output = gr.Markdown(label="View on Telegraph", visible=True)
82
  with gr.Row():
83
  summary_output = gr.Markdown(label="Parsed Content", visible=True)
 
 
84
 
85
  run_button.click(fn=generate_predictions,
86
+ inputs=[api_token_input, book_name_input, author_name_input, language_input, detail_options_input],
87
  outputs=[book_cover_output, summary_output, telegraph_link_output],
88
  show_progress=True, queue=True)
89
 
 
 
 
 
90
  examples = [
91
+ ["***", "Harry Potter and the Philosopher's Stone", "J.K. Rowling", "πŸ‡¬πŸ‡§ english"],
92
+ ["***", "Pride and Prejudice", "Jane Austen", "πŸ‡ΊπŸ‡¦ ukrainian"],
93
+ ["***", "The Great Gatsby", "F. Scott Fitzgerald", "πŸ‡«πŸ‡· french"]
94
  ]
95
+ gr.Examples(examples=examples, inputs=[api_token_input, book_name_input, author_name_input, language_input, detail_options_input])
96
  with gr.Tab("Talk about bookπŸŽ“"):
97
+ # Chat interface code remains unchanged
98
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  demo.launch(share=False)