File size: 2,732 Bytes
c46e62c
eac7abb
 
 
 
 
 
 
 
 
 
 
e3c7652
8bda472
16937bb
8bda472
16937bb
9686f63
eac7abb
 
 
 
 
 
f443a92
91dc355
eac7abb
 
 
 
 
 
 
f443a92
eac7abb
 
8dc7c18
eac7abb
 
 
 
 
 
9686f63
eac7abb
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
# from transformers import pipeline
# from transformers.utils import logging
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
import torch
from llama_index.core import VectorStoreIndex
from llama_index.core import Document
from llama_index.core import Settings
from llama_index.llms.huggingface import (
    HuggingFaceInferenceAPI,
    HuggingFaceLLM,
)
from huggingface_hub import login
#
HF_TOKEN = "wncSKewozDfuZCXCyFbYbAMHgUrfcrumkc"
#
login(token=("hf_" + HF_TOKEN))

#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. "
# " Ako ne znaš odgovor, reci da ne znaš, ne izmišljaj ga."
#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."

system_propmpt = "You are a friendly Chatbot."

# "facebook/blenderbot-400M-distill",  facebook/blenderbot-400M-distill, stabilityai/stablelm-zephyr-3b, BAAI/bge-small-en-v1.5
Settings.llm = HuggingFaceInferenceAPI(model_name="mistralai/Mistral-Nemo-Instruct-2407",
                              device_map="auto",
                              system_prompt = system_propmpt,
                              context_window=4096,
                              max_new_tokens=256,
#                              stopping_ids=[50278, 50279, 50277, 1, 0],
                              generate_kwargs={"temperature": 0.5, "do_sample": False},
#                              tokenizer_kwargs={"max_length": 4096},
                              tokenizer_name="mistralai/Mistral-Nemo-Instruct-2407", 
                             )

Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-m3")
documents = [Document(text="Indian parliament elections happened in April-May 2024. BJP Party won."),
             Document(text="Indian parliament elections happened in April-May 2021. XYZ Party won."),
             Document(text="Indian parliament elections happened in 2020. ABC Party won."),
            ]
index = VectorStoreIndex.from_documents(
    documents,
)

query_engine = index.as_query_engine()
def rag(input_text, file):
    return query_engine.query(
        input_text
    )

iface = gr.Interface(fn=rag, inputs=[gr.Textbox(label="Question", lines=6), gr.File()], 
                     outputs=[gr.Textbox(label="Result", lines=6)], 
                     title="Answer my question",
                     description= "CoolChatBot"
                    )
iface.launch()