krishna195 commited on
Commit
0ae4476
·
verified ·
1 Parent(s): 6eceb86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -54
app.py CHANGED
@@ -1,74 +1,86 @@
1
  import gradio as gr
 
 
2
  from llama_cpp import Llama
3
 
4
- # Load the Llama model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  llm = Llama.from_pretrained(
6
  repo_id="krishna195/second_guff",
7
  filename="unsloth.Q4_K_M.gguf",
8
  )
9
 
10
- # Define the chatbot function
11
- def chatbot_response(user_input):
12
- # System instructions
13
- system_prompt = """
14
- You are a chatbot specializing in recommending songs by the Estonian folk band **Curly Strings**.
15
- based on this anw the question and give the link of the source
16
- ## 🎵 **Song List**
17
- Here are some songs by Curly Strings:
18
- 1. **Kalakesed**
19
- 2. **Kus mu süda on ...**
20
- 3. **Vitsalaul**
21
- 4. **Viimases jaamas**
22
- 5. **Salaja**
23
- 6. **Üle ilma**
24
- 7. **Šveits**
25
- 8. **Kallimale**
26
- 9. **Üksteist peab hoidma**
27
- 10. **Suuda öelda ei**
28
- 11. **Annan käe**
29
- 12. **Tulbid ja Bonsai**
30
- 13. **Tüdruk Pika Kleidiga**
31
- 14. **Armasta mind (feat. Vaiko Eplik)**
32
- 15. **Minu, Pets, Margus ja Priit**
33
- 16. **Kauges külas**
34
- 17. **Tule ja jää**
35
- 18. **Kuutõbine**
36
- 19. **Omaenese ilus ja veas**
37
- 20. **Pulmad**
38
- 21. **Pillimeeste laul**
39
- 22. **Tehke ruumi!**
40
-
41
- ## 🎤 **Related Artists**
42
- If you enjoy Curly Strings, you might also like:
43
- - **Trad.Attack!**
44
- - **Eesti Raadio laululapsed**
45
- - **Körsikud**
46
- - **Karl-Erik Taukar**
47
- - **Dag**
48
- - **Sadamasild**
49
- - **Kruuv**
50
- - **Smilers**
51
- - **Mari Jürjens**
52
- - **Terminaator**
53
 
54
- ---
55
- """
 
 
 
 
 
 
 
56
 
57
- # Generate response from Llama model
58
  response = llm.create_chat_completion(
59
- messages=[
60
- {"role": "system", "content": system_prompt},
61
- {"role": "user", "content": user_input}
62
- ],
63
  temperature=0.5,
64
- max_tokens=1000, # Increased for better answers
65
  top_p=0.9,
66
  frequency_penalty=0.8,
67
  )
68
 
69
  return response["choices"][0]["message"]["content"].strip()
70
 
71
- # Create Gradio interface
72
  iface = gr.Interface(
73
  fn=chatbot_response,
74
  inputs=gr.Textbox(placeholder="Ask me about Curly Strings..."),
@@ -78,5 +90,5 @@ iface = gr.Interface(
78
  theme="compact",
79
  )
80
 
81
- # Launch the Gradio app
82
  iface.launch()
 
1
  import gradio as gr
2
+ import chromadb
3
+ from sentence_transformers import SentenceTransformer
4
  from llama_cpp import Llama
5
 
6
+ # Initialize ChromaDB
7
+ chroma_client = chromadb.PersistentClient(path="./chromadb_store")
8
+ collection = chroma_client.get_or_create_collection(name="curly_strings_knowledge")
9
+
10
+ # ✅ Load Local Embedding Model
11
+ embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
12
+
13
+ # ✅ Curly Strings Knowledge (Stored in ChromaDB as Vectors)
14
+ knowledge_base = [
15
+ {"id": "song_list", "text": """
16
+ Curly Strings is an Estonian folk band known for blending traditional and modern sounds.
17
+ Here are some of their popular songs:
18
+ 1. Kalakesed
19
+ 2. Kus mu süda on ...
20
+ 3. Vitsalaul
21
+ 4. Viimases jaamas
22
+ 5. Salaja
23
+ 6. Üle ilma
24
+ 7. Šveits
25
+ 8. Kallimale
26
+ 9. Üksteist peab hoidma
27
+ 10. Suuda öelda ei
28
+ """},
29
+ {"id": "related_artists", "text": """
30
+ If you enjoy Curly Strings, you might also like:
31
+ - Trad.Attack!
32
+ - Eesti Raadio laululapsed
33
+ - Körsikud
34
+ - Karl-Erik Taukar
35
+ - Dag
36
+ """},
37
+ {"id": "background", "text": """
38
+ Curly Strings started in Estonia and became famous for their unique blend of folk and contemporary music.
39
+ They often perform at international festivals and are known for their emotional and poetic lyrics.
40
+ """}
41
+ ]
42
+
43
+ # ✅ Store Knowledge in ChromaDB (If Not Already Stored)
44
+ existing_data = collection.get()
45
+ if not existing_data["ids"]:
46
+ for item in knowledge_base:
47
+ embedding = embedder.encode(item["text"]).tolist()
48
+ collection.add(documents=[item["text"]], embeddings=[embedding], ids=[item["id"]])
49
+
50
+ # ✅ Load Llama Model
51
  llm = Llama.from_pretrained(
52
  repo_id="krishna195/second_guff",
53
  filename="unsloth.Q4_K_M.gguf",
54
  )
55
 
56
+ # Function to Retrieve Relevant Knowledge
57
+ def retrieve_context(query):
58
+ query_embedding = embedder.encode(query).tolist()
59
+ results = collection.query(query_embeddings=[query_embedding], n_results=2)
60
+ retrieved_texts = [doc for doc in results.get("documents", []) if doc]
61
+ return "\n".join(retrieved_texts) if retrieved_texts else "No relevant data found."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ # ✅ Chatbot Function with ChromaDB-RAG
64
+ def chatbot_response(user_input):
65
+ context = retrieve_context(user_input) # Retrieve relevant info from ChromaDB
66
+
67
+ messages = [
68
+ {"role": "system", "content": "Use the knowledge retrieved to answer the user’s question."},
69
+ {"role": "user", "content": user_input},
70
+ {"role": "assistant", "content": f"Retrieved Context:\n{context}"},
71
+ ]
72
 
 
73
  response = llm.create_chat_completion(
74
+ messages=messages,
 
 
 
75
  temperature=0.5,
76
+ max_tokens=500,
77
  top_p=0.9,
78
  frequency_penalty=0.8,
79
  )
80
 
81
  return response["choices"][0]["message"]["content"].strip()
82
 
83
+ # Gradio UI
84
  iface = gr.Interface(
85
  fn=chatbot_response,
86
  inputs=gr.Textbox(placeholder="Ask me about Curly Strings..."),
 
90
  theme="compact",
91
  )
92
 
93
+ # Launch App
94
  iface.launch()