import gradio as gr from huggingface_hub import InferenceClient import os # Initialize Hugging Face Inference Client client = InferenceClient( model="meta-llama/Meta-Llama-3.1-8B-Instruct", token=os.getenv("HUGGINGFACEHUB_API_TOKEN") # Token is retrieved from the Hugging Face secret ) # Function to interact with the model def ask_model(question): responses = [] for message in client.chat_completion( messages=[{"role": "user", "content": question}], max_tokens=1000, stream=True, ): responses.append(message.choices[0].delta.content) return ''.join(responses) # Gradio Interface interface = gr.Interface( fn=ask_model, inputs=gr.Textbox(lines=2, placeholder="Ask a question..."), outputs="text", title="Llama3.1 Based Ai Chatbot", description="Ask the model any question and get a response. Try out other models [here](https://huggingface.co/tchans123)." ) # Launch Gradio app interface.launch(share=True)