File size: 2,240 Bytes
488f910
 
 
 
 
 
 
 
 
 
 
 
f496e5a
488f910
 
 
 
 
 
 
 
 
 
 
 
 
 
8a46061
488f910
8a46061
488f910
f496e5a
488f910
 
8a46061
488f910
 
8a46061
 
 
488f910
 
 
 
 
 
 
 
 
 
8a46061
488f910
 
 
8a46061
488f910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
from huggingface_hub import login
from dotenv import load_dotenv
from embedding import embeddings
from db.chroma import load_and_setup_db, search_cases
from chat.hermes_llm import ChatManager

# Load environment variables
load_dotenv()

# Login to Hugging Face
# login(token=os.getenv("HUGGINGFACEHUB_API_TOKEN"), add_to_git_credential=True)

# Initialize components
VECTOR_DB_PATH = os.getenv("VECTOR_DB_PATH")
vector_store = load_and_setup_db(VECTOR_DB_PATH, embeddings)
legal_chat = ChatManager(temperature=0.1)

def process_query(query, chat_history):
    try:
        # Search relevant cases
        results = search_cases(vectorstore=vector_store, query=query, k=1)
        response=None
        if len(results)>0:
        # Get response from chat manager
            response = legal_chat.get_response(results[0]['content'], query=query)
            response_final = f"""{response}\n\nkilde:[case_id:{results[0]['metadata']['case_id']}]"""
        else :
            response_final =" Det ønskede ord blev ikke fundet i nogen sager. Prøv met et andet søgeord"
        # Update chat history
        chat_history.append((query, response_final))
        return "", chat_history
    except Exception as e:
        return "", chat_history + [(query, f"Det ønskede ord blev ikke fundet i nogen sager. Prøv met et andet søgeord")]

# Create Gradio interface
with gr.Blocks(title="Jurai Insight") as demo:
    gr.Markdown("# Jurai Insight")
    gr.Markdown("Forudsig fremtiden, byg din sag på data.")
    
    chatbot = gr.Chatbot(
        [],
        elem_id="chatbot",
        bubble_full_width=False,
        height=400
    )
    
    with gr.Row():
        query_input = gr.Textbox(
            placeholder="Indtast dit spørgsmål her...",
            show_label=False,
            scale=4
        )
        submit_btn = gr.Button("Sende", scale=1)
    
 
    # Set up event handlers
    submit_btn.click(
        process_query,
        inputs=[query_input, chatbot],
        outputs=[query_input, chatbot]
    )
    query_input.submit(
        process_query,
        inputs=[query_input, chatbot],
        outputs=[query_input, chatbot]
    )

if __name__ == "__main__":
    demo.launch(share=True)