Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from typing import Optional, Tuple
|
3 |
+
from langchain.llm import LLMChain
|
4 |
+
from langchain.memory import ConversationBufferWindowMemory
|
5 |
+
from langchain.huggingface_hub import HuggingFaceHub
|
6 |
+
|
7 |
+
# Set up the language model chain
|
8 |
+
prompt = "Instructions: You are SplitticAI. You answer questions exactly like people ask them. You were made by SplitticHost. You impersonate yourself as an AI chatbot.\n\n"
|
9 |
+
llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":1e-10})
|
10 |
+
llm_chain = LLMChain(
|
11 |
+
llm=llm,
|
12 |
+
prompt=prompt,
|
13 |
+
verbose=True,
|
14 |
+
memory=ConversationBufferWindowMemory(k=2)
|
15 |
+
)
|
16 |
+
|
17 |
+
# Define the chat function
|
18 |
+
def chat(
|
19 |
+
inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain]
|
20 |
+
):
|
21 |
+
history = history or []
|
22 |
+
output = llm_chain.predict(human_input=inp)
|
23 |
+
history.append((inp, output))
|
24 |
+
return history, history
|
25 |
+
|
26 |
+
# Set up the Gradio interface
|
27 |
+
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
28 |
+
|
29 |
+
with block:
|
30 |
+
with gr.Row():
|
31 |
+
gr.Markdown("<h3><center>SplitticAI Chatbot</center></h3>")
|
32 |
+
|
33 |
+
chatbot = gr.Chatbot()
|
34 |
+
|
35 |
+
with gr.Row():
|
36 |
+
message = gr.Textbox(
|
37 |
+
label="What's your question?",
|
38 |
+
placeholder="What would you like to ask me?",
|
39 |
+
lines=1,
|
40 |
+
)
|
41 |
+
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
42 |
+
|
43 |
+
gr.Examples(
|
44 |
+
examples=[
|
45 |
+
"What is artificial intelligence?",
|
46 |
+
"How does SplitticAI work?",
|
47 |
+
"Can you tell me a joke?",
|
48 |
+
],
|
49 |
+
inputs=message,
|
50 |
+
)
|
51 |
+
|
52 |
+
gr.HTML("Ask SplitticAI anything and get an answer!")
|
53 |
+
|
54 |
+
gr.HTML(
|
55 |
+
"<center>Powered by SplitticHost</center>"
|
56 |
+
)
|
57 |
+
|
58 |
+
state = gr.State()
|
59 |
+
agent_state = gr.State()
|
60 |
+
|
61 |
+
submit.click(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
|
62 |
+
message.submit(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
|
63 |
+
|
64 |
+
block.launch(debug=True)
|