mgokg commited on
Commit
bdc45a6
·
verified ·
1 Parent(s): 4a37332

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -144
app.py CHANGED
@@ -7,42 +7,20 @@ from chromadb.config import DEFAULT_DATABASE, DEFAULT_TENANT
7
  from langchain.text_splitter import RecursiveCharacterTextSplitter
8
  import os
9
  import speech_recognition as sr
10
-
 
11
 
12
  # Initialisiere ChromaDB
13
  client_chroma = chromadb.Client()
14
- #client_croma = chromadb.PersistentClient(path="/")
15
  collection_name = "pdf_collection"
16
  collection = client_chroma.get_or_create_collection(name=collection_name)
17
 
18
- custom_css = """
19
- .gr-button {
20
- width: 300px; /* Set the width of the button */
21
- }
22
- """
23
-
24
  # Verwende die integrierten Embeddings von ChromaDB
25
  embedding_function = embedding_functions.DefaultEmbeddingFunction()
26
 
27
  def update(message):
28
- url = "https://api.groq.com/openai/v1/chat/completions"
29
- headers = {
30
- "Authorization": groq,
31
- "Content-Type": "application/json"
32
- }
33
- data = {
34
- "messages": [
35
- {
36
- "role": "user",
37
- "content": message
38
- }
39
- ],
40
- "model": "mixtral-8x7b-32768",
41
- "temperature": 0.2
42
- }
43
-
44
- response = requests.post(url, headers=headers, data=json.dumps(data))
45
- return response.json()['choices'][0]['message']['content']
46
 
47
  client = Client("Qwen/Qwen2.5-72B-Instruct")
48
 
@@ -53,121 +31,22 @@ def transcribe_audio(audio):
53
  audio_data = recognizer.record(source)
54
  try:
55
  text = recognizer.recognize_google(audio_data, language="de-DE")
56
- result = client.predict(
57
- query=text,
58
- history=[],
59
- system="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.",
60
- api_name="/model_chat"
61
- )
62
- result = result[1]
63
- result=gr.Markdown(result)
64
- return result
65
- #text = update(text)
66
- #return text
67
  except sr.UnknownValueError:
68
  return "Speech recognition could not understand the audio."
69
  except sr.RequestError as e:
70
  return f"Could not request results from Google Speech Recognition service; {e}"
71
 
 
72
 
73
-
74
-
75
-
76
- def ask_llm(llm_prompt_input):
77
- # Erstelle Embedding für den Prompt
78
- query_embedding = embedding_function([llm_prompt_input])[0]
79
-
80
- # Führe die Ähnlichkeitssuche durch
81
- results = collection.query(
82
- query_embeddings=[query_embedding],
83
- n_results=3
84
- )
85
-
86
- # Formatiere die Ergebnisse
87
- formatted_results = []
88
- for i, doc in enumerate(results["documents"][0]):
89
- metadata = results["metadatas"][0][i]
90
- filename = metadata["filename"]
91
- formatted_results.append(f"### Dokument {i+1} (Dateiname: {filename})\n{doc}\n")
92
-
93
- # Füge die formatierten Ergebnisse zum Prompt hinzu
94
- enriched_prompt = f"{llm_prompt_input}\n\n### Verwandte Informationen:\n{''.join(formatted_results)}"
95
- #print(enriched_prompt)
96
- # Führe die Abfrage des LLM durch
97
- result = client.predict(
98
- query=enriched_prompt,
99
- history=[],
100
- system="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.",
101
- api_name="/model_chat"
102
- )
103
- result = result[1]
104
- result=gr.Markdown(result)
105
- return result
106
-
107
- def process_pdf(file):
108
- # Read the PDF content
109
- pdf_reader = PdfReader(file.name)
110
- text = ""
111
- for page in pdf_reader.pages:
112
- text += page.extract_text()
113
-
114
- # Split the text into smaller chunks
115
- text_splitter = RecursiveCharacterTextSplitter(
116
- chunk_size=1000, # Adjust the chunk size as needed
117
- chunk_overlap=100 # Adjust the overlap as needed
118
- )
119
- chunks = text_splitter.split_text(text)
120
-
121
- # Create embeddings for each chunk
122
- embeddings = embedding_function(chunks)
123
-
124
- # Store each chunk in ChromaDB
125
- for i, chunk in enumerate(chunks):
126
- collection.add(
127
- documents=[chunk],
128
- metadatas=[{"filename": file.name, "chunk_id": i}],
129
- ids=[f"{file.name}_{i}"] # Use a unique ID for each chunk
130
- )
131
- return f"PDF wurde erfolgreich in ChromaDB gespeichert."
132
-
133
- # Example usage
134
- # process_pdf(your_file_object)
135
- def search_similar_documents(prompt):
136
- # Erstelle Embedding für den Prompt
137
- query_embedding = embedding_function([prompt])[0]
138
-
139
- # Führe die Ähnlichkeitssuche durch
140
- results = collection.query(
141
- query_embeddings=[query_embedding],
142
- n_results=3
143
- )
144
-
145
- # Formatiere die Ergebnisse
146
- formatted_results = []
147
- for i, doc in enumerate(results["documents"][0]):
148
- metadata = results["metadatas"][0][i]
149
- filename = metadata["filename"]
150
- formatted_results.append(f"{doc}\n")
151
-
152
- ergebnis = f"{''.join(formatted_results)}"
153
- ergebnis = gr.Markdown(ergebnis)
154
- return ergebnis
155
- #return "\n".join(formatted_results)
156
-
157
  with gr.Blocks() as chat:
158
  gr.Markdown("### Chat", elem_classes="tab-header")
159
- #with gr.Row():
160
- #prompt_input = gr.Textbox(label="Suche nach ähnlichen Dokumenten", placeholder="Gib einen Suchbegriff ein")
161
- #search_output = gr.Textbox(label="Ähnliche Dokumente")
162
- #with gr.Row():
163
- #search_button = gr.Button("Suchen")
164
- with gr.Row():
165
  llm_output = gr.Textbox(label="LLM Antwort")
166
  with gr.Row():
167
  llm_prompt_input = gr.Textbox(label="Frage an das LLM", placeholder="Gib eine Frage ein")
168
  llm_submit_button = gr.Button("send")
169
-
170
- #search_button.click(search_similar_documents, inputs=prompt_input, outputs=search_output)
171
  llm_submit_button.click(ask_llm, inputs=llm_prompt_input, outputs=llm_output)
172
 
173
  with gr.Blocks() as upload:
@@ -182,33 +61,21 @@ with gr.Blocks() as upload:
182
  with gr.Blocks() as suche:
183
  gr.Markdown("### suche", elem_classes="tab-header")
184
  with gr.Row():
185
- prompt_input = gr.Textbox(label="Suche nach ähnlichen Dokumenten", placeholder="Gib einen Suchbegriff ein")
186
  with gr.Row():
187
  search_output = gr.Textbox(label="Ähnliche Dokumente")
188
  with gr.Row():
189
  search_button = gr.Button("Suchen")
190
  search_button.click(search_similar_documents, inputs=prompt_input, outputs=search_output)
191
 
192
-
193
  with gr.Blocks() as speech:
194
  gr.Markdown("### audio", elem_classes="tab-header")
195
-
196
  with gr.Row():
197
  sr_inputs = gr.Microphone(type="filepath")
198
  sr_outputs = gr.Textbox(label="Transcribed Text")
199
-
200
- with gr.Row():
201
- submit_button = gr.Button("rec")
202
-
203
- submit_button.click(transcribe_audio, inputs=sr_inputs, outputs=sr_outputs)
204
-
205
 
206
- # Erstelle die Gradio-Schnittstelle
207
  with gr.Blocks() as demo:
208
- gr.TabbedInterface(
209
- [chat, upload, suche, speech]
210
- )
211
-
212
 
213
- # Starte die Gradio-Anwendung
214
  demo.launch()
 
7
  from langchain.text_splitter import RecursiveCharacterTextSplitter
8
  import os
9
  import speech_recognition as sr
10
+ import requests
11
+ import json
12
 
13
  # Initialisiere ChromaDB
14
  client_chroma = chromadb.Client()
 
15
  collection_name = "pdf_collection"
16
  collection = client_chroma.get_or_create_collection(name=collection_name)
17
 
 
 
 
 
 
 
18
  # Verwende die integrierten Embeddings von ChromaDB
19
  embedding_function = embedding_functions.DefaultEmbeddingFunction()
20
 
21
  def update(message):
22
+ # Your update function implementation
23
+ pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  client = Client("Qwen/Qwen2.5-72B-Instruct")
26
 
 
31
  audio_data = recognizer.record(source)
32
  try:
33
  text = recognizer.recognize_google(audio_data, language="de-DE")
34
+ # Process the transcribed text as needed
35
+ return text
 
 
 
 
 
 
 
 
 
36
  except sr.UnknownValueError:
37
  return "Speech recognition could not understand the audio."
38
  except sr.RequestError as e:
39
  return f"Could not request results from Google Speech Recognition service; {e}"
40
 
41
+ # Other functions (ask_llm, process_pdf, search_similar_documents) remain unchanged
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  with gr.Blocks() as chat:
44
  gr.Markdown("### Chat", elem_classes="tab-header")
45
+ with gr.Row():
 
 
 
 
 
46
  llm_output = gr.Textbox(label="LLM Antwort")
47
  with gr.Row():
48
  llm_prompt_input = gr.Textbox(label="Frage an das LLM", placeholder="Gib eine Frage ein")
49
  llm_submit_button = gr.Button("send")
 
 
50
  llm_submit_button.click(ask_llm, inputs=llm_prompt_input, outputs=llm_output)
51
 
52
  with gr.Blocks() as upload:
 
61
  with gr.Blocks() as suche:
62
  gr.Markdown("### suche", elem_classes="tab-header")
63
  with gr.Row():
64
+ prompt_input = gr.Textbox(label="Suche nach ähnlichen Dokumenten", placeholder="Gib einen Suchbegriff ein")
65
  with gr.Row():
66
  search_output = gr.Textbox(label="Ähnliche Dokumente")
67
  with gr.Row():
68
  search_button = gr.Button("Suchen")
69
  search_button.click(search_similar_documents, inputs=prompt_input, outputs=search_output)
70
 
 
71
  with gr.Blocks() as speech:
72
  gr.Markdown("### audio", elem_classes="tab-header")
 
73
  with gr.Row():
74
  sr_inputs = gr.Microphone(type="filepath")
75
  sr_outputs = gr.Textbox(label="Transcribed Text")
76
+ sr_inputs.change(transcribe_audio, inputs=sr_inputs, outputs=sr_outputs)
 
 
 
 
 
77
 
 
78
  with gr.Blocks() as demo:
79
+ gr.TabbedInterface([chat, upload, suche, speech])
 
 
 
80
 
 
81
  demo.launch()