Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,108 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def respond(
|
11 |
message,
|
12 |
-
history
|
13 |
system_message,
|
14 |
max_tokens,
|
15 |
temperature,
|
16 |
top_p,
|
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 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
gr.Slider(
|
53 |
minimum=0.1,
|
54 |
maximum=1.0,
|
55 |
value=0.95,
|
56 |
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)"
|
58 |
),
|
59 |
],
|
|
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
import gradio as gr
|
4 |
+
import spaces
|
5 |
from huggingface_hub import InferenceClient
|
6 |
+
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
|
7 |
+
from langchain_community.vectorstores import Chroma
|
8 |
+
from langchain.prompts import PromptTemplate
|
9 |
|
10 |
+
# Configure ZeroGPU client
|
11 |
+
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
|
|
|
|
12 |
|
13 |
+
# Initialize embeddings
|
14 |
+
embeddings = HuggingFaceInstructEmbeddings(
|
15 |
+
model_name="hkunlp/instructor-base",
|
16 |
+
model_kwargs={"device": "cpu"} # Use CPU for Spaces
|
17 |
+
)
|
18 |
+
|
19 |
+
# Load the persisted database
|
20 |
+
db = Chroma(
|
21 |
+
persist_directory="db",
|
22 |
+
embedding_function=embeddings
|
23 |
+
)
|
24 |
|
25 |
+
# Prompt templates
|
26 |
+
DEFAULT_SYSTEM_PROMPT = """
|
27 |
+
You are a ROS2 expert assistant. Based on the information provided in the context, answer questions
|
28 |
+
accurately and concisely. If the information is not in the context, acknowledge that you don't know.
|
29 |
+
""".strip()
|
30 |
+
|
31 |
+
@spaces.GPU(duration=60)
|
32 |
def respond(
|
33 |
message,
|
34 |
+
history,
|
35 |
system_message,
|
36 |
max_tokens,
|
37 |
temperature,
|
38 |
top_p,
|
39 |
):
|
40 |
+
try:
|
41 |
+
# Retrieve relevant context
|
42 |
+
docs = db.similarity_search(message, k=2)
|
43 |
+
context = "\n".join([doc.page_content for doc in docs])
|
44 |
+
|
45 |
+
# Build messages
|
46 |
+
messages = [{"role": "system", "content": system_message}]
|
47 |
+
for val in history:
|
48 |
+
if val[0]:
|
49 |
+
messages.append({"role": "user", "content": val[0]})
|
50 |
+
if val[1]:
|
51 |
+
messages.append({"role": "assistant", "content": val[1]})
|
52 |
+
|
53 |
+
# Add context to the user message
|
54 |
+
augmented_message = f"Context: {context}\n\nQuestion: {message}"
|
55 |
+
messages.append({"role": "user", "content": augmented_message})
|
56 |
+
|
57 |
+
# Stream the response
|
58 |
+
response = ""
|
59 |
+
for message in client.chat_completion(
|
60 |
+
messages,
|
61 |
+
max_tokens=max_tokens,
|
62 |
+
stream=True,
|
63 |
+
temperature=temperature,
|
64 |
+
top_p=top_p,
|
65 |
+
):
|
66 |
+
token = message.choices[0].delta.content
|
67 |
+
response += token
|
68 |
+
yield response
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
yield f"An error occurred: {str(e)}"
|
72 |
|
73 |
+
# Create Gradio interface
|
|
|
|
|
|
|
74 |
demo = gr.ChatInterface(
|
75 |
respond,
|
76 |
additional_inputs=[
|
77 |
+
gr.Textbox(
|
78 |
+
value=DEFAULT_SYSTEM_PROMPT,
|
79 |
+
label="System message"
|
80 |
+
),
|
81 |
+
gr.Slider(
|
82 |
+
minimum=1,
|
83 |
+
maximum=2048,
|
84 |
+
value=500,
|
85 |
+
step=1,
|
86 |
+
label="Max new tokens"
|
87 |
+
),
|
88 |
+
gr.Slider(
|
89 |
+
minimum=0.1,
|
90 |
+
maximum=4.0,
|
91 |
+
value=0.1,
|
92 |
+
step=0.1,
|
93 |
+
label="Temperature"
|
94 |
+
),
|
95 |
gr.Slider(
|
96 |
minimum=0.1,
|
97 |
maximum=1.0,
|
98 |
value=0.95,
|
99 |
step=0.05,
|
100 |
+
label="Top-p (nucleus sampling)"
|
101 |
),
|
102 |
],
|
103 |
+
title="ROS2 Expert Assistant",
|
104 |
+
description="Ask questions about ROS2, navigation, and robotics. I'll answer based on my knowledge base.",
|
105 |
)
|
106 |
|
|
|
107 |
if __name__ == "__main__":
|
108 |
+
demo.launch()
|