Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
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 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
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 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
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 |
+
)
|