Spaces:
Sleeping
Sleeping
Commit
·
98b18ce
1
Parent(s):
2563ddc
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,15 +9,17 @@ template = """You are a chatbot having a conversation with a human.
|
|
| 9 |
{chat_history}
|
| 10 |
Human: {human_input}
|
| 11 |
Chatbot:"""
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
def generate_response(question, huggingfacehub_api_token, temperature=0.6, max_new_tokens=500):
|
| 15 |
try:
|
| 16 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 17 |
-
llm = HuggingFaceHub(
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
llm_chain = LLMChain(prompt=prompt, llm=llm, memory=memory)
|
| 22 |
response = llm_chain.predict(chat_history="", human_input=question)
|
| 23 |
except ValueError as e:
|
|
@@ -25,24 +27,31 @@ def generate_response(question, huggingfacehub_api_token, temperature=0.6, max_n
|
|
| 25 |
print(f"Error: {str(e)}")
|
| 26 |
return response
|
| 27 |
|
|
|
|
| 28 |
inputs = [
|
| 29 |
gr.inputs.Textbox(label="Question"),
|
| 30 |
gr.inputs.Textbox(label="HuggingFace API Token", type="password", default=None),
|
| 31 |
gr.inputs.Slider(minimum=0.1, maximum=2.0, default=0.6, label="Temperature"),
|
| 32 |
-
gr.inputs.Slider(minimum=100, maximum=1000, default=500, label="Max New Tokens")
|
| 33 |
]
|
| 34 |
-
outputs = gr.outputs.Textbox(label="Response")
|
| 35 |
|
| 36 |
-
|
| 37 |
-
description = "Provide a question and get helpful answers from the AI assistant."
|
| 38 |
-
examples = [["write a poem on Iron Man"], ["What are the benefits of using Python?"]]
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
allow_flagging="never",
|
| 46 |
-
examples=examples)
|
| 47 |
|
| 48 |
-
iface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
{chat_history}
|
| 10 |
Human: {human_input}
|
| 11 |
Chatbot:"""
|
| 12 |
+
|
| 13 |
+
prompt = PromptTemplate(template=template, input_variables=["chat_history", "human_input"])
|
| 14 |
|
| 15 |
def generate_response(question, huggingfacehub_api_token, temperature=0.6, max_new_tokens=500):
|
| 16 |
try:
|
| 17 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 18 |
+
llm = HuggingFaceHub(
|
| 19 |
+
huggingfacehub_api_token=huggingfacehub_api_token,
|
| 20 |
+
repo_id=repo_id,
|
| 21 |
+
model_kwargs={"temperature": temperature, "max_new_tokens": max_new_tokens},
|
| 22 |
+
)
|
| 23 |
llm_chain = LLMChain(prompt=prompt, llm=llm, memory=memory)
|
| 24 |
response = llm_chain.predict(chat_history="", human_input=question)
|
| 25 |
except ValueError as e:
|
|
|
|
| 27 |
print(f"Error: {str(e)}")
|
| 28 |
return response
|
| 29 |
|
| 30 |
+
|
| 31 |
inputs = [
|
| 32 |
gr.inputs.Textbox(label="Question"),
|
| 33 |
gr.inputs.Textbox(label="HuggingFace API Token", type="password", default=None),
|
| 34 |
gr.inputs.Slider(minimum=0.1, maximum=2.0, default=0.6, label="Temperature"),
|
| 35 |
+
gr.inputs.Slider(minimum=100, maximum=1000, default=500, label="Max New Tokens"),
|
| 36 |
]
|
|
|
|
| 37 |
|
| 38 |
+
outputs = gr.outputs.HTML(label="Response")
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
title = "Chatbot Interface [BETA]"
|
| 41 |
+
description = "Provide a question and get helpful answers from the AI assistant. Create your huggingface account and get your API token from https://huggingface.co/settings/token."
|
| 42 |
+
theme=gr.themes.Base(primary_hue="blue", secondary_hue="blue")
|
| 43 |
+
|
| 44 |
+
examples = [["write a poem on Iron Man"], ["What are the benefits of using Python?"]]
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=generate_response,
|
| 48 |
+
inputs=inputs,
|
| 49 |
+
outputs=outputs,
|
| 50 |
+
theme=theme,
|
| 51 |
+
title=title,
|
| 52 |
+
description=description,
|
| 53 |
+
allow_flagging="never",
|
| 54 |
+
examples=examples,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
iface.launch(debug=True, show_api=False)
|