Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
top_p,
|
| 14 |
-
):
|
| 15 |
-
messages = [{"role": "system", "content": system_message}]
|
| 16 |
-
for val in history:
|
| 17 |
-
if val[0]:
|
| 18 |
-
messages.append({"role": "user", "content": val[0]})
|
| 19 |
-
if val[1]:
|
| 20 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": message})
|
| 23 |
-
|
| 24 |
-
response = ""
|
| 25 |
-
|
| 26 |
-
for message in client.chat_completion(
|
| 27 |
-
messages,
|
| 28 |
-
max_tokens=max_tokens,
|
| 29 |
-
stream=True,
|
| 30 |
-
temperature=temperature,
|
| 31 |
-
top_p=top_p,
|
| 32 |
-
):
|
| 33 |
-
token = message.choices[0].delta.content
|
| 34 |
-
|
| 35 |
-
response += token
|
| 36 |
-
yield response
|
| 37 |
|
| 38 |
-
""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|