krishna195 commited on
Commit
26ac60a
·
verified ·
1 Parent(s): 1a6ff18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -70
app.py CHANGED
@@ -1,89 +1,72 @@
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 (Fixed Nested List Issue)
57
- def retrieve_context(query):
58
- query_embedding = embedder.encode(query).tolist()
59
- results = collection.query(query_embeddings=[query_embedding], n_results=2)
60
-
61
- # Flatten nested lists and ensure only strings are returned
62
- retrieved_texts = [doc for sublist in results.get("documents", []) for doc in sublist if isinstance(doc, str)]
63
-
64
- return "\n".join(retrieved_texts) if retrieved_texts else "No relevant data found."
65
-
66
- # ✅ Chatbot Function with ChromaDB-RAG
67
  def chatbot_response(user_input):
68
- context = retrieve_context(user_input) # Retrieve relevant info from ChromaDB
69
-
70
- messages = [
71
- {"role": "system", "content": "Use the knowledge retrieved to answer the user’s question."},
72
- {"role": "user", "content": user_input},
73
- {"role": "assistant", "content": f"Retrieved Context:\n{context}"},
74
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
 
76
  response = llm.create_chat_completion(
77
- messages=messages,
 
 
 
78
  temperature=0.5,
79
- max_tokens=500,
80
  top_p=0.9,
81
  frequency_penalty=0.8,
82
  )
83
 
84
  return response["choices"][0]["message"]["content"].strip()
85
 
86
- # Gradio UI
87
  iface = gr.Interface(
88
  fn=chatbot_response,
89
  inputs=gr.Textbox(placeholder="Ask me about Curly Strings..."),
@@ -93,5 +76,5 @@ iface = gr.Interface(
93
  theme="compact",
94
  )
95
 
96
- # Launch App
97
- iface.launch()
 
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
+ ## 🎤 **Related Artists**
41
+ If you enjoy Curly Strings, you might also like:
42
+ - **Trad.Attack!**
43
+ - **Eesti Raadio laululapsed**
44
+ - **Körsikud**
45
+ - **Karl-Erik Taukar**
46
+ - **Dag**
47
+ - **Sadamasild**
48
+ - **Kruuv**
49
+ - **Smilers**
50
+ - **Mari Jürjens**
51
+ - **Terminaator**
52
+ ---
53
+ """
54
 
55
+ # Generate response from Llama model
56
  response = llm.create_chat_completion(
57
+ messages=[
58
+ {"role": "system", "content": system_prompt},
59
+ {"role": "user", "content": user_input}
60
+ ],
61
  temperature=0.5,
62
+ max_tokens=1000, # Increased for better answers
63
  top_p=0.9,
64
  frequency_penalty=0.8,
65
  )
66
 
67
  return response["choices"][0]["message"]["content"].strip()
68
 
69
+ # Create Gradio interface
70
  iface = gr.Interface(
71
  fn=chatbot_response,
72
  inputs=gr.Textbox(placeholder="Ask me about Curly Strings..."),
 
76
  theme="compact",
77
  )
78
 
79
+ # Launch the Gradio app
80
+ iface.launch()