Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Create app_llama_index.py
Browse files- app_llama_index.py +52 -0
    	
        app_llama_index.py
    ADDED
    
    | @@ -0,0 +1,52 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import gradio as gr
         | 
| 2 | 
            +
            # from transformers import pipeline
         | 
| 3 | 
            +
            # from transformers.utils import logging
         | 
| 4 | 
            +
            from llama_index.embeddings.huggingface import HuggingFaceEmbedding
         | 
| 5 | 
            +
            import torch
         | 
| 6 | 
            +
            from llama_index.core import VectorStoreIndex
         | 
| 7 | 
            +
            from llama_index.core import Document
         | 
| 8 | 
            +
            from llama_index.core import Settings
         | 
| 9 | 
            +
            from llama_index.llms.huggingface import (
         | 
| 10 | 
            +
                HuggingFaceInferenceAPI,
         | 
| 11 | 
            +
                HuggingFaceLLM,
         | 
| 12 | 
            +
            )
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            #system_sr = "Zoveš se U-Chat AI asistent i pomažeš korisniku usluga kompanije United Group. Korisnik postavlja pitanje ili problem, upareno sa dodatnima saznanjima. Na osnovu toga napiši korisniku kratak i ljubazan odgovor koji kompletira njegov zahtev ili mu daje odgovor na pitanje. "
         | 
| 15 | 
            +
            # " Ako ne znaš odgovor, reci da ne znaš, ne izmišljaj ga."
         | 
| 16 | 
            +
            #system_sr += "Usluge kompanije United Group uključuju i kablovsku mrežu za digitalnu televiziju, pristup internetu, uređaj EON SMART BOX za TV sadržaj, kao i fiksnu telefoniju."
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            system_propmpt = "You are a friendly Chatbot."
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            # "facebook/blenderbot-400M-distill",  facebook/blenderbot-400M-distill , BAAI/bge-small-en-v1.5
         | 
| 21 | 
            +
            Settings.llm = HuggingFaceLLM(model_name="stabilityai/stablelm-zephyr-3b",
         | 
| 22 | 
            +
                                          device_map="auto",
         | 
| 23 | 
            +
                                          system_prompt = system_propmpt,
         | 
| 24 | 
            +
                                          context_window=4096,
         | 
| 25 | 
            +
                                          max_new_tokens=256,
         | 
| 26 | 
            +
            #                              stopping_ids=[50278, 50279, 50277, 1, 0],
         | 
| 27 | 
            +
                                          generate_kwargs={"temperature": 0.5, "do_sample": False},
         | 
| 28 | 
            +
            #                              tokenizer_kwargs={"max_length": 4096},
         | 
| 29 | 
            +
                                          tokenizer_name="stabilityai/stablelm-zephyr-3b", 
         | 
| 30 | 
            +
                                         )
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            Settings.embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
         | 
| 33 | 
            +
            documents = [Document(text="Indian parliament elections happened in April-May 2024. BJP Party won."),
         | 
| 34 | 
            +
                         Document(text="Indian parliament elections happened in April-May 2021. XYZ Party won."),
         | 
| 35 | 
            +
                         Document(text="Indian parliament elections happened in 2020. ABC Party won."),
         | 
| 36 | 
            +
                        ]
         | 
| 37 | 
            +
            index = VectorStoreIndex.from_documents(
         | 
| 38 | 
            +
                documents,
         | 
| 39 | 
            +
            )
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            query_engine = index.as_query_engine()
         | 
| 42 | 
            +
            def rag(input_text, file):
         | 
| 43 | 
            +
                return query_engine.query(
         | 
| 44 | 
            +
                    input_text
         | 
| 45 | 
            +
                )
         | 
| 46 | 
            +
             | 
| 47 | 
            +
            iface = gr.Interface(fn=rag, inputs=[gr.Textbox(label="Question", lines=6), gr.File()], 
         | 
| 48 | 
            +
                                 outputs=[gr.Textbox(label="Result", lines=6)], 
         | 
| 49 | 
            +
                                 title="Answer my question",
         | 
| 50 | 
            +
                                 description= "CoolChatBot"
         | 
| 51 | 
            +
                                )
         | 
| 52 | 
            +
            iface.launch()
         | 

