HonestAnnie commited on
Commit
2e8a9c7
1 Parent(s): def34f2

jetzt aber

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -1,9 +1,7 @@
1
  import os
2
  import gradio as gr
3
  import chromadb
4
-
5
  from sentence_transformers import SentenceTransformer
6
-
7
  import spaces
8
 
9
  @spaces.GPU
@@ -15,12 +13,12 @@ def get_embeddings(query, task):
15
 
16
  # Initialize a persistent Chroma client and retrieve collection
17
  client = chromadb.PersistentClient(path="./chroma")
18
- collection = client.get_collection(name="phil_de")
19
-
20
- authors_list = ["Ludwig Wittgenstein", "Sigmund Freud", "Marcus Aurelius", "Friedrich Nietzsche", "Epiktet", "Ernst Jünger", "Georg Christoph Lichtenberg", "Balthasar Gracian", "Hannah Arendt", "Erich Fromm", "Albert Camus"]
21
- #authors_list = ["Friedrich Nietzsche", "Joscha Bach"]
22
 
23
- def query_chroma(embeddings, authors, num_results=10):
24
  try:
25
  where_filter = {"author": {"$in": authors}} if authors else {}
26
 
@@ -57,10 +55,11 @@ def query_chroma(embeddings, authors, num_results=10):
57
 
58
 
59
  # Main function
60
- def perform_query(query, authors, num_results):
61
  task = "Given a question, retrieve passages that answer the question"
62
  embeddings = get_embeddings(query, task)
63
- results = query_chroma(embeddings, authors, num_results)
 
64
 
65
  if "error" in results:
66
  return [gr.update(visible=True, value=f"Error: {results['error']}") for _ in range(max_textboxes * 2)]
@@ -74,6 +73,9 @@ def perform_query(query, authors, num_results):
74
 
75
  return updates
76
 
 
 
 
77
  # Gradio interface
78
  max_textboxes = 30
79
 
@@ -81,25 +83,29 @@ with gr.Blocks(css=".custom-markdown { border: 1px solid #ccc; padding: 10px; bo
81
  gr.Markdown("Enter your query, filter authors (default is all), click **Search** to search. Click **Flag** if a result is relevant to the query and interesting to you.")
82
  with gr.Row():
83
  with gr.Column():
84
- inp = gr.Textbox(label="query", placeholder="Enter thought...")
85
- author_inp = gr.Dropdown(label="authors", choices=authors_list, multiselect=True)
 
86
  num_results_inp = gr.Number(label="number of results", value=10, step=1, minimum=1, maximum=max_textboxes)
87
  btn = gr.Button("Search")
88
 
89
  components = []
90
- textboxes = []
91
 
92
  for _ in range(max_textboxes):
93
  with gr.Column() as col:
94
  text_out = gr.Markdown(visible=False, elem_classes="custom-markdown")
95
  components.append(text_out)
96
- textboxes.append(text_out)
97
 
98
  btn.click(
99
  fn=perform_query,
100
- inputs=[inp, author_inp, num_results_inp],
101
  outputs=components
102
  )
103
 
 
 
 
 
 
104
 
105
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
  import chromadb
 
4
  from sentence_transformers import SentenceTransformer
 
5
  import spaces
6
 
7
  @spaces.GPU
 
13
 
14
  # Initialize a persistent Chroma client and retrieve collection
15
  client = chromadb.PersistentClient(path="./chroma")
16
+ collection_de = client.get_collection(name="phil_de")
17
+ collection_en = client.get_collection(name="phil_en")
18
+ authors_list_de = ["Ludwig Wittgenstein", "Sigmund Freud", "Marcus Aurelius", "Friedrich Nietzsche", "Epiktet", "Ernst Jünger", "Georg Christoph Lichtenberg", "Balthasar Gracian", "Hannah Arendt", "Erich Fromm", "Albert Camus"]
19
+ authors_list_en = ["Friedrich Nietzsche", "Joscha Bach"]
20
 
21
+ def query_chroma(collection, embeddings, authors, num_results=10):
22
  try:
23
  where_filter = {"author": {"$in": authors}} if authors else {}
24
 
 
55
 
56
 
57
  # Main function
58
+ def perform_query(query, authors, num_results, database):
59
  task = "Given a question, retrieve passages that answer the question"
60
  embeddings = get_embeddings(query, task)
61
+ collection = collection_de if database == "German" else collection_en
62
+ results = query_chroma(collection, embeddings, authors, num_results)
63
 
64
  if "error" in results:
65
  return [gr.update(visible=True, value=f"Error: {results['error']}") for _ in range(max_textboxes * 2)]
 
73
 
74
  return updates
75
 
76
+ def update_authors(database):
77
+ return gr.update(choices=authors_list_de if database == "German" else authors_list_en)
78
+
79
  # Gradio interface
80
  max_textboxes = 30
81
 
 
83
  gr.Markdown("Enter your query, filter authors (default is all), click **Search** to search. Click **Flag** if a result is relevant to the query and interesting to you.")
84
  with gr.Row():
85
  with gr.Column():
86
+ database_inp = gr.Dropdown(label="Database", choices=["English", "German"], value="German")
87
+ inp = gr.Textbox(label="query", placeholder="Enter question...")
88
+ author_inp = gr.Dropdown(label="authors", choices=authors_list_de, multiselect=True)
89
  num_results_inp = gr.Number(label="number of results", value=10, step=1, minimum=1, maximum=max_textboxes)
90
  btn = gr.Button("Search")
91
 
92
  components = []
 
93
 
94
  for _ in range(max_textboxes):
95
  with gr.Column() as col:
96
  text_out = gr.Markdown(visible=False, elem_classes="custom-markdown")
97
  components.append(text_out)
 
98
 
99
  btn.click(
100
  fn=perform_query,
101
+ inputs=[inp, author_inp, num_results_inp, database_inp],
102
  outputs=components
103
  )
104
 
105
+ database_inp.change(
106
+ fn=update_authors,
107
+ inputs=database_inp,
108
+ outputs=author_inp
109
+ )
110
 
111
  demo.launch()