import time import gradio as gr from groq import Groq import os system_prompt = """ You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. Your response should be complete, and not end abruptly. """ groq_api_key = os.environ.get("groq_api_key") client = Groq( api_key=groq_api_key ) conversation_history = [{"role": "system", "content": system_prompt}] def chat_with_llama(prompt): global conversation_history conversation_history.append({"role": "user", "content": prompt}) chat_completion = client.chat.completions.create( messages=conversation_history, model="llama-3.1-8b-instant", ) response = chat_completion.choices[0].message.content conversation_history.append({"role": "assistant", "content": response}) # Simulate streaming effect streamed_response = "" for i in range(1, len(response) + 1): streamed_response = response[:i] yield streamed_response time.sleep(0.01) # Adjust the sleep time to control the streaming speed def clear_fields(): global conversation_history conversation_history = [{"role": "system", "content": system_prompt}] return "", "" # Clear prompt and output, but not the API key #demo = gr.Interface(fn=chat_with_Mixtral, inputs=[gr.Textbox(label="Question"),gr.Textbox(label="Hugging Face API Key", type="password")],outputs="text") with gr.Blocks(theme='freddyaboulton/test-blue') as demo: gr.Markdown("

Arjun's Chatbot

") gr.Markdown("Hi there! I'm an AI assistant tasked with answering one question at a time. I'm instructed to provide complete and truthful responses, without any harmful or biased content. I have some memory issues which are work in progress, so I cannot remember previous conversations, yet!. Happy Chatting!") prompt = gr.Textbox(label='Question', lines=2, max_lines=5, placeholder = 'Type your question here.') conversation_display = gr.Textbox(label='Conversation History', lines=20, max_lines=30, placeholder='Conversation history will appear here...', interactive=False) def update_conversation(prompt, history): new_response = chat_with_llama(prompt) updated_history = history + f"\nUser: {prompt}\n\nAssistant: " for partial_response in new_response: updated_history = history + f"\nUser: {prompt}\n\nAssistant: {partial_response}" yield gr.update(value=updated_history) with gr.Group(): with gr.Row(): submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm") clear_btn = gr.ClearButton(value="Clear Question and AI Response", elem_id="clear_button", variant="secondary", size="sm") submit_btn.click(fn=update_conversation, inputs = [prompt,conversation_display], outputs=[conversation_display]) clear_btn.click(fn=clear_fields,outputs=[prompt,conversation_display]) demo.launch() #test comment