File size: 2,829 Bytes
728d085
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import chromadb
import gradio as gr
from sentence_transformers import SentenceTransformer
from llama_cpp import Llama

# ✅ Initialize ChromaDB
chroma_client = chromadb.PersistentClient(path="./chromadb_store")
collection = chroma_client.get_or_create_collection(name="curly_strings_knowledge")

# ✅ Load Local Embedding Model
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

# ✅ Load Fine-Tuned LLaMA Model
llm = Llama.from_pretrained(
    repo_id="krishna195/second_guff",
    filename="unsloth.Q4_K_M.gguf",
)

# ✅ File-Based Search Function
def search_in_file(query, file_path="merged_output.txt"):
    try:
        with open(file_path, "r", encoding="utf-8") as file:
            lines = file.readlines()
        
        # Search for the query in file content
        matched_lines = [line.strip() for line in lines if query.lower() in line.lower()]
        
        return "\n".join(matched_lines) if matched_lines else "No relevant data found in file."
    
    except FileNotFoundError:
        return "File not found. Please check the file path."

# ✅ Retrieve Context from ChromaDB & File
def retrieve_context(query):
    query_embedding = embedder.encode(query).tolist()
    results = collection.query(query_embeddings=[query_embedding], n_results=2)

    retrieved_texts = [doc for sublist in results.get("documents", []) for doc in sublist if isinstance(doc, str)]
    
    # If no result from ChromaDB, try searching in the file
    if not retrieved_texts:
        return search_in_file(query)
    
    return "\n".join(retrieved_texts)

# ✅ Chatbot Function with Optimized Retrieval
def chatbot_response(user_input):
    context = retrieve_context(user_input)

    messages = [
        {"role": "system", "content": """You are an expert on the Estonian folk band Curly Strings.  
        - Use the **retrieved knowledge** from ChromaDB or the file to answer.  
        - If a **song** is mentioned, provide its name and **suggest similar tracks**.  
        - If no match is found, say "I couldn’t find details, but here’s what I know."."""},
        {"role": "user", "content": user_input},
        {"role": "assistant", "content": f"Retrieved Context:\n{context}"},
    ]

    response = llm.create_chat_completion(
        messages=messages,
        temperature=0.4,  
        max_tokens=300,
        top_p=0.9,
        frequency_penalty=0.7,
    )

    return response["choices"][0]["message"]["content"].strip()

# ✅ Gradio Chatbot Interface
iface = gr.Interface(
    fn=chatbot_response,
    inputs=gr.Textbox(label="Ask me about Curly Strings 🎻"),
    outputs=gr.Textbox(label="Bot Response 🎶"),
    title="Curly Strings Chatbot",
    description="Ask about the Estonian folk band Curly Strings! Now also searches in 'merged_output.txt'.",
)

iface.launch()