Spaces:
Running
Running
import gradio as gr | |
# from transformers import pipeline | |
# from transformers.utils import logging | |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader | |
from llama_index.vector_stores.chroma import ChromaVectorStore | |
from llama_index.core import StorageContext | |
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 | |
import chromadb as chromadb | |
from chromadb.utils import embedding_functions | |
import shutil | |
import os | |
from io import StringIO | |
# | |
last = 0 | |
CHROMA_DATA_PATH = "chroma_data/" | |
EMBED_MODEL = "BAAI/bge-m3" | |
# all-MiniLM-L6-v2 | |
CHUNK_SIZE = 800 | |
CHUNK_OVERLAP = 50 | |
max_results = 3 | |
min_len = 40 | |
min_distance = 0.35 | |
max_distance = 0.6 | |
temperature = 0.55 | |
max_tokens=3072 | |
top_p=0.8 | |
frequency_penalty=0.0 | |
presence_penalty=0.15 | |
jezik = "srpski" | |
cs = "s0" | |
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. " | |
chroma_client = chromadb.PersistentClient(CHROMA_DATA_PATH) | |
embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction( | |
model_name=EMBED_MODEL | |
) | |
collection = chroma_client.get_or_create_collection( | |
name="chroma_data", | |
embedding_function=embedding_func, | |
metadata={"hnsw:space": "cosine"}, | |
) | |
last = collection.count() | |
# | |
HF_TOKEN = "wncSKewozDfuZCXCyFbYbAMHgUrfcrumkc" | |
# | |
login(token=("hf_" + HF_TOKEN)) | |
system_propmpt = system_sr | |
# "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", | |
) | |
# "BAAI/bge-m3" | |
Settings.embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2") | |
#documents = [Document(text="Indian parliament elections happened in April-May 2024. BJP Party won."), | |
# ] | |
#index = VectorStoreIndex.from_documents( | |
# documents, | |
#) | |
vector_store = ChromaVectorStore(chroma_collection=collection) | |
index = VectorStoreIndex.from_vector_store(vector_store, embed_model=Settings.embed_model) | |
query_engine = index.as_query_engine( | |
similarity_top_k=3, | |
vector_store_query_mode="default", | |
# filters=MetadataFilters( | |
# filters=[ | |
# ExactMatchFilter(key="state", value=cs), | |
# ] | |
# ), | |
alpha=None, | |
doc_ids=None, | |
) | |
chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True) | |
def upload_file(filepath): | |
documents = SimpleDirectoryReader(filepath).load_data() | |
index = VectorStoreIndex.from_documents(documents) | |
#query_engine = index.as_query_engine() | |
chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True) | |
return filepath | |
def resetChat(): | |
chat_engine.reset() | |
return True | |
def rag(input_text, history, jezik, file): | |
# if (btn): | |
# resetChat() | |
print(history, input_text) | |
if (file): | |
# documents = [] | |
# for f in file: | |
# documents += SimpleDirectoryReader(f).load_data() | |
# f = file + "*.pdf" | |
pathname = os.path.dirname | |
# shutil.copyfile(file.name, path) | |
print("pathname=", pathname) | |
print("basename=", os.path.basename(file)) | |
print("filename=", file.name) | |
documents = SimpleDirectoryReader(file.name).load_data() | |
index2 = VectorStoreIndex.from_documents(documents) | |
query_engine = index2.as_query_engine() | |
# return query_engine.query(input_text) | |
# return history.append({"role": "assistant", "content": query_engine.query(input_text)}) | |
return history.append( | |
ChatMessage(role="assistant", | |
content=query_engine.query(input_text) | |
) | |
# collection.add( | |
# documents=documents, | |
# ids=[f"id{last+i}" for i in range(len(documents))], | |
# metadatas=[{"state": "s0", "next": "s0", "used": False, "source": 'None', "page": -1, "lang": jezik } for i in range(len(documents)) ] | |
# ) | |
else: | |
o_jezik = "N/A" | |
match jezik: | |
case 'hrvatski': | |
o_jezik = 'na hrvatskom jeziku' | |
Settings.llm.system_prompt = system_sr + "Call centar telefon je 095 1000 444 za privatne i 095 1000 500 za poslovne korisnike. Stranica podrške je <https://tele mach.hr/podrska>." + "Odgovaraj " + o_jezik | |
case 'slovenski': | |
o_jezik = 'v slovenščini' | |
Settings.llm.system_prompt = system_sr + "Call centar i pomoč za fizične uporabnike: 070 700 700.stran za podporo je <https://telemach.si/pomoc>. " + "Odgovor " + o_jezik | |
case 'srpski': | |
o_jezik = 'na srpskom jeziku' | |
Settings.llm.system_prompt = system_sr + "Call centar telefon je 19900 za sve korisnike. Stranica podrške je <https://sbb.rs/podrska/>. " + "Odgovaraj " + o_jezik | |
case 'makedonski': | |
o_jezik = 'на македонски јазикот' | |
Settings.llm.system_prompt = system_sr + "Stranica podrške je https://mn.nettvplus.com/me/podrska/ za NetTV. " + "Oдговори " + o_jezik | |
case 'Eksperimentalna opcija': | |
o_jezik = 'N/A' | |
Settings.llm.system_prompt = system_sr + "Call centar telefon je 12755 za Crnu Goru, 0800 31111 za BIH, 070 700 700 u Sloveniji, 19900 u Srbiji, 095 1000 444 za hrvatske korisnike. " | |
# if (o_jezik!='N/A'): | |
# input_text += " - odgovori " + o_jezik + "." | |
# return query_engine.query(input_text) | |
return history.append( | |
ChatMessage(role="assistant", | |
content=chat_engine.chat(input_text)} | |
) | |
# Interface | |
# gr.Textbox(label="Pitanje:", lines=6), | |
# outputs=[gr.Textbox(label="Odgovor:", lines=6)], | |
iface = gr.ChatInterface(rag, | |
title="UChat", | |
description="Postavite pitanje ili opišite problem koji imate", | |
chatbot=gr.Chatbot(ChatMessage(role="assistant", content="Kako Vam mogu pomoći?"), type='messages', label="Uchat", height=300), | |
textbox=gr.Textbox(placeholder="Pitanje ili opis problema", container=False, scale=7), | |
theme="soft", | |
# examples=["Ne radi mi internet", "Koje usluge imam na raspologanju?", "Ne radi mi daljinski upravljač, šta da radim?"], | |
# cache_examples=True, | |
retry_btn=None, | |
undo_btn="Briši prethodno", | |
clear_btn="Briši sve", | |
additional_inputs = [gr.Dropdown(["slovenski", "hrvatski", "srpski", "makedonski", "Eksperimentalna opcija"], value="srpski", label="Jezik", info="N/A"), | |
gr.File()] | |
) | |
#with gr.Blocks() as iface: | |
# gr.Markdown("Uchat") | |
# file_out = gr.File() | |
# with gr.Row(): | |
# with gr.Column(scale=1): | |
# inp = gr.Textbox(label="Pitanje:", lines=6) | |
# u = gr.UploadButton("Upload a file", file_count="single") | |
# with gr.Column(scale=1): | |
# out = gr.Textbox(label="Odgovor:", lines=6) | |
# sub = gr.Button("Pokreni") | |
# | |
# u.upload(upload_file, u, file_out) | |
# sub.click(rag, inp, out) | |
iface.launch() |