Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,68 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
"""
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import pickle
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
from pypdf import PdfReader
|
7 |
+
import re
|
8 |
+
import google.generativeai as genai
|
9 |
|
10 |
+
# Ρύθμιση του Gemini API
|
11 |
+
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
|
|
|
|
12 |
|
13 |
+
# Φόρτωση του SentenceTransformer
|
14 |
+
st_model = SentenceTransformer('dimitriz/st-greek-media-bert-base-uncased')
|
15 |
|
16 |
+
# Συνάρτηση για το cosine similarity
|
17 |
+
def cosine_similarity(a, b):
|
18 |
+
return np.dot(a, b.T) / (np.linalg.norm(a, axis=1)[:, np.newaxis] * np.linalg.norm(b, axis=1))
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
# Αντικατάσταση μοτίβων στο κείμενο
|
21 |
+
def replace_pattern(text, pattern=r" ν\. (\d+)", replacement=r" ν.\1"):
|
22 |
+
return re.sub(pattern, replacement, text)
|
|
|
|
|
23 |
|
24 |
+
# Δημιουργία chunks και ενσωματώσεων
|
25 |
+
if os.path.exists('embedded_data.pkl'):
|
26 |
+
with open('embedded_data.pkl', 'rb') as file:
|
27 |
+
chunks, embedded_data = pickle.load(file)
|
28 |
+
else:
|
29 |
+
chunks = []
|
30 |
+
for file in os.listdir("archive"):
|
31 |
+
if file.endswith(".pdf"):
|
32 |
+
reader = PdfReader(f'archive/{file}')
|
33 |
+
text = "".join([page.extract_text() for page in reader.pages])
|
34 |
+
text = replace_pattern(text, pattern=r" ν\. (\d+)", replacement=r" ν.\1")
|
35 |
+
chunks += text.split("\n") # Απλό splitting
|
36 |
+
embedded_data = st_model.encode(chunks)
|
37 |
+
with open('embedded_data.pkl', 'wb') as file:
|
38 |
+
pickle.dump((chunks, embedded_data), file)
|
39 |
|
40 |
+
# Συνάρτηση RAG για ερώτημα χρήστη
|
41 |
+
def rag_response(query):
|
42 |
+
embedded_query = st_model.encode(query)
|
43 |
+
similarities = cosine_similarity(embedded_query[np.newaxis, :], embedded_data)
|
44 |
+
top_indices = np.argsort(similarities[0])[::-1][:20]
|
45 |
+
top_doct = [chunks[index] for index in top_indices]
|
46 |
|
47 |
+
augmented_prompt = f"""Είσαι ένα σύστημα εμπειρογνώμονας και απαντάς ερωτήσεις.
|
48 |
+
Θα σου δώσω μια ερώτηση και ένα πλαίσιο και θα επιστρέψεις την απάντηση.
|
49 |
+
Ερώτηση : {query} Πλαίσιο : {top_doct}"""
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
try:
|
52 |
+
response = genai.generate_content(augmented_prompt)
|
53 |
+
return response.text
|
54 |
+
except Exception as e:
|
55 |
+
return f"Σφάλμα: {str(e)}"
|
56 |
|
57 |
+
# Δημιουργία Gradio UI
|
58 |
+
interface = gr.Interface(
|
59 |
+
fn=rag_response,
|
60 |
+
inputs=gr.Textbox(label="Η ερώτησή σας:", lines=2, placeholder="Πληκτρολογήστε την ερώτησή σας εδώ..."),
|
61 |
+
outputs=gr.Textbox(label="Απάντηση από το Gemini:"),
|
62 |
+
title="RAG Gemini Assistant",
|
63 |
+
description="Χρησιμοποιεί RAG για να παρέχει ακριβείς και τεκμηριωμένες απαντήσεις."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
)
|
65 |
|
66 |
+
# Εκκίνηση της εφαρμογής
|
67 |
if __name__ == "__main__":
|
68 |
+
interface.launch(share=True)
|