shoom013 commited on
Commit
eac7abb
1 Parent(s): a47c3c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -53
app.py CHANGED
@@ -1,56 +1,52 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
-
6
-
7
- def respond(
8
- message,
9
- history: list[tuple[str, str]],
10
- system_message,
11
- max_tokens,
12
- temperature,
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
- https://docs.llamaindex.ai/en/stable/examples/customization/llms/SimpleIndexDemo-Huggingface_stablelm/
40
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
41
- """
42
- demo = gr.ChatInterface(
43
- respond,
44
- additional_inputs=[
45
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
46
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
47
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
48
- gr.Slider(
49
- minimum=0.1,
50
- maximum=1.0,
51
- value=0.95,
52
- step=0.05,
53
- label="Top-p (nucleus sampling)",
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()