Spaces:
Running
Running
Samuel Thomas
commited on
Commit
·
043dc9e
1
Parent(s):
8e741ad
initial commit
Browse files
app.py
CHANGED
@@ -1,64 +1,124 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
"""
|
5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
"""
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
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 |
-
|
48 |
-
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
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 gradio as gr
|
2 |
+
from typing import TypedDict, Annotated
|
3 |
+
from huggingface_hub import InferenceClient, login, list_models
|
4 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFacePipeline
|
5 |
+
from langgraph.graph.message import add_messages
|
6 |
+
from langchain.docstore.document import Document
|
7 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
8 |
+
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
|
9 |
+
from langchain_community.retrievers import BM25Retriever
|
10 |
+
import os
|
11 |
+
from langgraph.graph import START, StateGraph
|
12 |
+
from langchain.tools import Tool
|
13 |
"""
|
14 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
15 |
"""
|
16 |
+
HUGGINGFACEHUB_API_TOKEN = os.environ["HUGGINGFACEHUB_API_TOKEN"]
|
17 |
+
login(token=HUGGINGFACEHUB_API_TOKEN, add_to_git_credential=True)
|
18 |
+
|
19 |
+
llm = HuggingFaceEndpoint(
|
20 |
+
#repo_id="HuggingFaceH4/zephyr-7b-beta",
|
21 |
+
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
22 |
+
task="text-generation",
|
23 |
+
max_new_tokens=512,
|
24 |
+
do_sample=False,
|
25 |
+
repetition_penalty=1.03,
|
26 |
+
timeout=240,
|
27 |
+
)
|
28 |
+
|
29 |
+
model = ChatHuggingFace(llm=llm, verbose=True)
|
30 |
|
31 |
+
def get_hub_stats(author: str) -> str:
|
32 |
+
"""
|
33 |
+
You are a helpful chatbot for programmers and data scientists with access to the Hugging Face Hub.
|
34 |
+
Users will want to know the most popular models from Hugging Face. This tool will enable
|
35 |
+
you to fetch the most downloaded model from a specific author on the Hugging Face Hub.
|
36 |
+
"""
|
37 |
+
try:
|
38 |
+
# List models from the specified author, sorted by downloads
|
39 |
+
models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
|
40 |
+
|
41 |
+
if models:
|
42 |
+
model = models[0]
|
43 |
+
return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
|
44 |
+
else:
|
45 |
+
return f"No models found for author {author}."
|
46 |
+
except Exception as e:
|
47 |
+
return f"Error fetching models for {author}: {str(e)}"
|
48 |
+
|
49 |
+
# Initialize the tool
|
50 |
+
hub_stats_tool = Tool(
|
51 |
+
name="get_hub_stats",
|
52 |
+
func=get_hub_stats,
|
53 |
+
description="Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|
54 |
+
)
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
def predict(message, history):
|
58 |
+
# Convert Gradio history to LangChain message format
|
59 |
+
history_langchain_format = []
|
60 |
+
for msg in history:
|
61 |
+
if msg['role'] == "user":
|
62 |
+
history_langchain_format.append(HumanMessage(content=msg['content']))
|
63 |
+
elif msg['role'] == "assistant":
|
64 |
+
history_langchain_format.append(AIMessage(content=msg['content']))
|
65 |
+
|
66 |
+
# Add new user message
|
67 |
+
history_langchain_format.append(HumanMessage(content=message))
|
68 |
+
|
69 |
+
# Invoke Alfred agent with full message history
|
70 |
+
response = alfred.invoke(
|
71 |
+
input={"messages": history_langchain_format},
|
72 |
+
config={"recursion_limit": 100}
|
73 |
+
)
|
74 |
+
|
75 |
+
# Extract final assistant message
|
76 |
+
return response["messages"][-1].content
|
77 |
|
|
|
78 |
|
79 |
+
# setup agents
|
80 |
+
tools = [hub_stats_tool]
|
81 |
+
#tools = [guest_info_tool]
|
82 |
+
chat_with_tools = model.bind_tools(tools)
|
83 |
|
84 |
+
# Generate the AgentState and Agent graph
|
85 |
+
class AgentState(TypedDict):
|
86 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
+
def assistant(state: AgentState):
|
89 |
+
return {
|
90 |
+
"messages": [chat_with_tools.invoke(state["messages"])],
|
91 |
+
}
|
92 |
+
|
93 |
+
## The graph
|
94 |
+
builder = StateGraph(AgentState)
|
95 |
+
|
96 |
+
# Define nodes: these do the work
|
97 |
+
builder.add_node("assistant", assistant)
|
98 |
+
builder.add_node("tools", ToolNode(tools))
|
99 |
+
|
100 |
+
# Define edges: these determine how the control flow moves
|
101 |
+
builder.add_edge(START, "assistant")
|
102 |
+
builder.add_conditional_edges(
|
103 |
+
"assistant",
|
104 |
+
# If the latest message requires a tool, route to tools
|
105 |
+
# Otherwise, provide a direct response
|
106 |
+
tools_condition,
|
107 |
+
)
|
108 |
+
builder.add_edge("tools", "assistant")
|
109 |
+
alfred = builder.compile()
|
110 |
|
111 |
|
112 |
"""
|
113 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
114 |
"""
|
115 |
demo = gr.ChatInterface(
|
116 |
+
predict,
|
117 |
+
type="messages"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
)
|
119 |
|
120 |
|
121 |
if __name__ == "__main__":
|
122 |
demo.launch()
|
123 |
+
|
124 |
+
|