File size: 7,842 Bytes
c29df11
3b67edb
dbd949c
 
e9a8280
c0c400e
67bfd1d
6fdac26
12181b6
54c3e95
3749801
54c3e95
dbd949c
de4f158
3bff9be
dbd949c
e9a8280
d189514
3bff9be
 
 
 
 
 
dbd949c
 
69e614b
96ff633
8c6505a
 
 
 
9d4d8fc
 
 
 
 
 
8c6505a
9d4d8fc
 
 
 
 
 
3749801
986ca64
 
 
 
3749801
6c70e53
c05c5ab
12181b6
 
 
 
 
 
cacfc38
9d4d8fc
69e614b
3bff9be
 
 
 
 
 
61c237b
 
179374d
3f29601
b9040a1
 
179374d
 
ea2a3a1
3bff9be
12181b6
 
 
 
 
3bff9be
 
 
 
 
 
 
c6213ad
3bff9be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b144175
2080a5e
3bff9be
 
 
 
 
 
e9a8280
aab0a99
4e6d3b1
6d0f2de
3bff9be
 
e9a8280
96ff633
b144175
2080a5e
b144175
 
 
 
d1d9757
b144175
 
85718b7
 
 
3bff9be
85718b7
 
 
 
33b764f
12181b6
3bff9be
ec648be
ea2a3a1
 
12181b6
ea2a3a1
a4acfbd
dc49965
fd52b07
 
 
ea2a3a1
 
 
 
 
 
 
85718b7
3bff9be
b144175
3bff9be
 
 
 
b144175
3bff9be
f18293c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import gradio as gr
import chromadb
from chromadb.utils import embedding_functions
from PyPDF2 import PdfReader
from gradio_client import Client
from chromadb.config import DEFAULT_DATABASE, DEFAULT_TENANT
from langchain.text_splitter import RecursiveCharacterTextSplitter
import os
import speech_recognition as sr
import groq
import pyttsx3
api_key = os.getenv('groq')
# Initialisiere ChromaDB
client_chroma = chromadb.Client()
#client_croma = chromadb.PersistentClient(path="/")
collection_name = "pdf_collection"
collection = client_chroma.get_or_create_collection(name=collection_name)

custom_css = """
.gr-button {
    width: 300px;  /* Set the width of the button */
}
"""

# Verwende die integrierten Embeddings von ChromaDB
embedding_function = embedding_functions.DefaultEmbeddingFunction()
#client = Client("Qwen/Qwen2.5-72B-Instruct")


def update(message):
    client = groq.Client(api_key=api_key)
    
    try:
        # Use Llama 3 70B powered by Groq for text generation
        completion = client.chat.completions.create(
            model="llama3-70b-8192",
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": f"{message} antworte immer auf deutsch"}
            ],
        )
        return completion.choices[0].message.content
    except Exception as e:
        return f"Error in response generation: {str(e)}"


#def text_to_speech(text):
    #engine = pyttsx3.init()
    #engine.say(text)
    #engine.runAndWait()



# Function to transcribe audio data to text
def transcribe_audio(audio):
    recognizer = sr.Recognizer()
    with sr.AudioFile(audio) as source:
        audio_data = recognizer.record(source)
        try:
            text = recognizer.recognize_google(audio_data, language="de-DE")
            result = update(text)
            return result
            result = client.predict(
                query=text,
                history=[],
                system="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.",
            api_name="/model_chat"
    )
            result = result[1]
            result = result[0][1]
            result=gr.Markdown(result)
            #tts = gTTS(text=result, lang='de')
            #tts.save("./chroma/output.mp3")
            #os.system("start ./chroma/output.mp3")  # Dies spielt die Audiodatei ab
            #text_to_speech(result)
            return result
            #text = update(text)
            #return text
        except sr.UnknownValueError:
            return "Speech recognition could not understand the audio."
        except sr.RequestError as e:
            return f"Could not request results from Google Speech Recognition service; {e}"

def ask_llm(llm_prompt_input):
    # Erstelle Embedding für den Prompt
    query_embedding = embedding_function([llm_prompt_input])[0]
    
    # Führe die Ähnlichkeitssuche durch
    results = collection.query(
        query_embeddings=[query_embedding],
        n_results=1
    )
    
    # Formatiere die Ergebnisse
    formatted_results = []
    for i, doc in enumerate(results["documents"][0]):
        metadata = results["metadatas"][0][i]
        filename = metadata["filename"]
        formatted_results.append(f"### Dokument {i+1} (Dateiname: {filename})\n{doc}\n")
    
    # Füge die formatierten Ergebnisse zum Prompt hinzu
    enriched_prompt = f"{llm_prompt_input}\n\n### Verwandte Informationen:\n{''.join(formatted_results)}"
    #print(enriched_prompt)
    # Führe die Abfrage des LLM durch
    result = client.predict(
        query=enriched_prompt,
        history=[],
        system="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.",
        api_name="/model_chat"
    )
    result = result[1]
    result=gr.Markdown(result)   
    return result
 
def process_pdf(file):
    # Read the PDF content
    pdf_reader = PdfReader(file.name)
    text = ""
    for page in pdf_reader.pages:
        text += page.extract_text()
    
    # Split the text into smaller chunks
    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size=1000,  # Adjust the chunk size as needed
        chunk_overlap=100  # Adjust the overlap as needed
    )
    chunks = text_splitter.split_text(text)
    
    # Create embeddings for each chunk
    embeddings = embedding_function(chunks)
    
    # Store each chunk in ChromaDB
    for i, chunk in enumerate(chunks):
        collection.add(
            documents=[chunk],
            metadatas=[{"filename": file.name, "chunk_id": i}],
            ids=[f"{file.name}_{i}"]  # Use a unique ID for each chunk
        )
    return f"PDF wurde erfolgreich in ChromaDB gespeichert."

# Example usage
# process_pdf(your_file_object)
def search_similar_documents(prompt):
    # Erstelle Embedding für den Prompt
    query_embedding = embedding_function([prompt])[0]
    
    # Führe die Ähnlichkeitssuche durch
    results = collection.query(
        query_embeddings=[query_embedding],
        n_results=3
    )
    
    # Formatiere die Ergebnisse
    formatted_results = []
    for i, doc in enumerate(results["documents"][0]):
        metadata = results["metadatas"][0][i]
        filename = metadata["filename"]
        formatted_results.append(f"{doc}\n")

    ergebnis = f"{''.join(formatted_results)}"
    ergebnis = gr.Markdown(ergebnis)
    return ergebnis
    #return "\n".join(formatted_results)
   
with gr.Blocks() as chat:
    gr.Markdown("### Chat", elem_classes="tab-header")
    #with gr.Row():
        #prompt_input = gr.Textbox(label="Suche nach ähnlichen Dokumenten", placeholder="Gib einen Suchbegriff ein")
        #search_output = gr.Textbox(label="Ähnliche Dokumente")
    #with gr.Row():
        #search_button = gr.Button("Suchen")
    with gr.Row():       
        llm_output = gr.Textbox(label="LLM Antwort")
    with gr.Row():
        llm_prompt_input = gr.Textbox(label="Frage an das LLM", placeholder="Gib eine Frage ein")
        llm_submit_button = gr.Button("send")
    
    #search_button.click(search_similar_documents, inputs=prompt_input, outputs=search_output)
    llm_submit_button.click(ask_llm, inputs=llm_prompt_input, outputs=llm_output)

with gr.Blocks() as upload:
    gr.Markdown("### upload", elem_classes="tab-header")
    with gr.Row():
        file_input = gr.File(label="Wähle eine PDF-Datei aus", type="filepath")
        upload_output = gr.Textbox(label="Upload Status")
    with gr.Row():
        submit_button = gr.Button("upload")
    submit_button.click(process_pdf, inputs=file_input, outputs=upload_output)

with gr.Blocks() as suche:
    gr.Markdown("### suche", elem_classes="tab-header")
    with gr.Row():
        prompt_input = gr.Textbox(label="Suche nach ähnlichen Dokumenten", placeholder="Gib einen Suchbegriff ein")        
    with gr.Row():
        search_output = gr.Textbox(label="Ähnliche Dokumente")
    with gr.Row():
        search_button = gr.Button("Suchen")
    search_button.click(search_similar_documents, inputs=prompt_input, outputs=search_output)


with gr.Blocks() as speech:
    gr.Markdown("### audio", elem_classes="tab-header")
    
    with gr.Row():
        sr_outputs = gr.Textbox(label="Antwort")
    with gr.Row():    
        sr_inputs = gr.Microphone(type="filepath")
    #with gr.Row():
        #audio_button = gr.Button("audio")
    #audio_button.click(text_to_speech("guten tag wie geht es dir"))    
    sr_inputs.change(transcribe_audio, inputs=sr_inputs, outputs=sr_outputs)
    
    #with gr.Row():
        #submit_button = gr.Button("rec")
    
    #submit_button.click(transcribe_audio, inputs=sr_inputs, outputs=sr_outputs)
 

# Erstelle die Gradio-Schnittstelle
with gr.Blocks() as demo:
    gr.TabbedInterface(
        [chat, upload, suche, speech]
    )


# Starte die Gradio-Anwendung
demo.launch()