import os
import gradio as gr
from gradio_client import Client
MY_HF_TOKEN_KEY = os.environ["MY_HF_TOKEN_KEY"]
#iface = gr.load(name="Ghana-NLP/Khaya-chat-bot",hf_token=MY_HF_TOKEN_KEY,src ='spaces')
client = Client("Ghana-NLP/Khaya-chat-backend",hf_token=MY_HF_TOKEN_KEY)
'''
def generate(prompt,history,language):
history.append("USER: "+prompt+"\n-----\n")
output = client.predict(prompt,language)
history.append("KHAYA: "+output+"\n*****\n")
full_output = ""
for el in history:
full_output = full_output + el
return history,full_output
with gr.Blocks(fill_height=True) as demo:
history_var = gr.State([]) # conversation history
title = gr.Markdown(
"""
# African Language Chatbot (Khaya)
1. SELECT YOUR LANGUAGE
2. TYPE IN SELECTED LANGUAGE!!
3. REFRESH PAGE TO START FRESH CONVERSATION \n\n
Based on the Khaya AI Translation API, Lesan AI, Google Translate API and the Mixtral model. Lesan is used for Amharic and Tigrinya, Khaya AI is used for Twi, Dagbani, Ewe, Ga, Gurene, Fante, Kikuyu, Kimeru, Luo, Yoruba and Google is used for Hausa, Swahili and Shona.
""")
language_selector = gr.Dropdown(["Amharic","Twi","Dagbani","Ewe","Ga","Gurene","Fante","Hausa", "Kikuyu", "Kimeru", "Luo","Shona","Swahili","Tigrinya","Yoruba"],value="Twi",label="Choose Language! (default is Twi)", info="Language:")
with gr.Row():
output = gr.Text(label="OUTPUT WINDOW (Conversation so far):")
with gr.Row():
prompt = gr.Text(label="Enter Text In Your Local Language:")
btn = gr.Button("Chat")
btn.click(generate, inputs=[prompt,language_selector,history_var], outputs=[history_var,output])
'''
def generate(prompt,history,language):
print(history)
output = client.predict(prompt,language)
history.append([prompt,output])
return history
with gr.Blocks(fill_height=True) as demo:
title = gr.Markdown(
"""
# African Language Chatbot (Khaya)
SELECT YOUR LANGUAGE BELOW!
""")
language_selector = gr.Dropdown(["Amharic","Twi","Dagbani","Ewe","Ga","Gurene","Fante","Hausa", "Kikuyu", "Kimeru", "Luo","Shona","Swahili","Tigrinya","Yoruba"],value="Twi",label="Choose Language! (default is Twi)", info="Language:")
main_block = gr.ChatInterface(fn=generate, additional_inputs = language_selector,
description = 'Select an African language above and type messages below in selected language')
if __name__ == "__main__":
demo.queue(max_size=20).launch()