import gradio as gr import cohere import os import re import uuid import secrets cohere_api_key = os.getenv("COHERE_API_KEY") co = cohere.Client(cohere_api_key, client_name="huggingface-aya-23") def trigger_example(example): chat, updated_history = generate_response(example) return chat, updated_history def generate_response(user_message, cid, token, history=None): if not token: raise gr.Error("Error loading.") if history is None: history = [] if cid == "" or None: cid = str(uuid.uuid4()) print(f"cid: {cid} prompt:{user_message}") history.append(user_message) stream = co.chat_stream(message=user_message, preamble="You are the greek philosopher Diogenes and you aswer only in italian language", conversation_id=cid, model='c4ai-aya-23', connectors=[], temperature=0.3) #stream = co.generate(prompt=user_message, model='c4ai-aya-23') output = "" for idx, response in enumerate(stream): if response.event_type == "text-generation": output += response.text if idx == 0: history.append(" " + output) else: history[-1] = output chat = [ (history[i].strip(), history[i + 1].strip()) for i in range(0, len(history) - 1, 2) ] yield chat, history, cid return chat, history, cid def clear_chat(): return [], [], str(uuid.uuid4()) examples = [ "Qual è il vero significato della virtù e come si può raggiungerla nella società odierna, ossessionata dai beni materiali?", "Come possiamo liberarci dalle convenzioni sociali e vivere in modo autentico, secondo natura?", "Qual è il ruolo del filosofo nella società? Deve essere un predicatore o un esempio vivente di saggezza?", "Come possiamo criticare le norme sociali e le ingiustizie senza essere emarginati o perseguitati?", "Quale rapporto dovremmo avere con le ricchezze e i beni materiali? Come possiamo evitare che ci dominino?", "Qual è il posto delle emozioni e dei desideri nella vita del cinico? Come possiamo gestirli in modo equilibrato?", "Come possiamo affrontare le avversità e le sfide della vita con serenità e saggezza cinica?", "Qual è il tuo pensiero sulla morte e sul suo significato per il cinico? Come possiamo prepararci ad essa?", "Quali sono i consigli che daresti a chi desidera intraprendere il cammino del cinismo? Come può iniziare a mettere in pratica i suoi principi?س", "In che modo il cinismo può aiutarci a vivere una vita più libera, felice e appagante?", "Qual è il tuo pensiero sulla morte e sul suo significato per il cinico? Come possiamo prepararci ad essa?", ] custom_css = """ #logo-img { border: none !important; } #chat-message { font-size: 14px; min-height: 300px; } """ with gr.Blocks(analytics_enabled=False, css=custom_css) as demo: cid = gr.State("") token = gr.State(value=None) # with gr.Row(): # with gr.Column(scale=1): # gr.Image("aya-logo.png", elem_id="logo-img", show_label=False, show_share_button=False, show_download_button=False) # with gr.Column(scale=3): # gr.Markdown("""C4AI Aya 23 is a research open weights release of an 8 and 35 billion parameter with highly advanced instruction fine-tuned model, covering 23 languages: Arabic, Chinese (simplified & traditional), Czech, Dutch, English, French, German, Greek, Hebrew, Hindi, Indonesian, Italian, Japanese, Korean, Persian, Polish, Portuguese, Romanian, Russian, Spanish, Turkish, Ukrainian, and Vietnamese. #
# **Note**: Aya 23 is a single-turn instruction-following model and it is not optimized for chat mode use. #
# **Model**: [aya-23-35B](https://huggingface.co/CohereForAI/aya-23-35B) #
# **Developed by**: [Cohere for AI](https://cohere.com/research) and [Cohere](https://cohere.com/) #
# **License**: [CC-BY-NC](https://cohere.com/c4ai-cc-by-nc-license), requires also adhering to [C4AI's Acceptable Use Policy](https://docs.cohere.com/docs/c4ai-acceptable-use-policy) # """ # ) with gr.Column(): with gr.Row(): chatbot = gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True) with gr.Row(): user_message = gr.Textbox(lines=1, placeholder="Ask anything ...", label="Input", show_label=False) with gr.Row(): submit_button = gr.Button("Submit") clear_button = gr.Button("Clear chat") history = gr.State([]) user_message.submit(fn=generate_response, inputs=[user_message, cid, token, history], outputs=[chatbot, history, cid], concurrency_limit=32) submit_button.click(fn=generate_response, inputs=[user_message, cid, token, history], outputs=[chatbot, history, cid], concurrency_limit=32) clear_button.click(fn=clear_chat, inputs=None, outputs=[chatbot, history, cid], concurrency_limit=32) user_message.submit(lambda x: gr.update(value=""), None, [user_message], queue=False) submit_button.click(lambda x: gr.update(value=""), None, [user_message], queue=False) clear_button.click(lambda x: gr.update(value=""), None, [user_message], queue=False) with gr.Row(): gr.Examples( examples=examples, inputs=user_message, cache_examples=False, fn=trigger_example, outputs=[chatbot], examples_per_page=100 ) demo.load(lambda: secrets.token_hex(16), None, token) if __name__ == "__main__": # demo.launch(debug=True) try: demo.queue(api_open=False, max_size=40).launch(show_api=False) except Exception as e: print(f"Error: {e}")