Spaces:
Build error
Build error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import streamlit as stfrom huggingface_hub import InferenceClient"""For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference"""client = InferenceClient()def respond(message, history, system_message, max_tokens, temperature, top_p):messages = [{"role": "system", "content": system_message}]for user_msg, assistant_msg in history:if user_msg:messages.append({"role": "user", "content": user_msg})if assistant_msg:messages.append({"role": "assistant", "content": assistant_msg})messages.append({"role": "user", "content": message})response = ""try:for message in client.chat_completion(model="mistralai/Codestral-22B-v0.1",messages=messages,max_tokens=max_tokens,stream=True,temperature=temperature,top_p=top_p,):token = message.choices[0].delta.contentresponse += tokenyield responseexcept Exception as e:yield f"Error: {e}"# Streamlit interfacest.title("Chat with Codestral Model")system_message = st.text_input("System message", value="You are an expert python coder with in-depth knowledge of langchain.")max_tokens = st.slider("Max new tokens", min_value=1, max_value=2048, value=2048, step=1)temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.6, step=0.1)top_p = st.slider("Top-p (nucleus sampling)", min_value=0.1, max_value=1.0, value=0.95, step=0.05)# Initialize history in session stateif "history" not in st.session_state:st.session_state.history = []def get_response():user_input = st.session_state.user_inputif user_input:st.session_state.history.append((user_input, ""))response_generator = respond(user_input, st.session_state.history, system_message, max_tokens, temperature, top_p)response = ""for r in response_generator:response = rst.session_state.history[-1] = (user_input, response)st.session_state.user_input = "" # Clear input after sending# Display chat historyst.text_area("Chat History", value="\n".join([f"User: {h[0]}\nAssistant: {h[1]}" for h in st.session_state.history]), height=300, key="chat_history")# User inputst.text_input("Your message:", key="user_input", on_change=get_response)123456789101112131415161718192021222324252627282930313233343536# Import necessary librariesimport streamlit as stfrom transformers import pipeline, AutoTokenizer, AutoModelForCausalLM# Load the model and tokenizermodel_name = "mistralai/Codestral-22B-v0.1"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name)# Initialize the pipelinetext_generator = pipeline("text-generation", model=model, tokenizer=tokenizer)# Streamlit interfacest.title("Codestral Text Generation")st.write("""This is a text generation application using the Codestral model from Mistral AI.Enter your prompt below and generate text.""")# User inputuser_input = st.text_area("Enter your prompt here:", "")if st.button("Generate"):if user_input:with st.spinner("Generating text..."):# Generate text using the modelgenerated_text = text_generator(user_input, max_length=100, num_return_sequences=1)st.write("### Generated Text")st.write(generated_text[0]['generated_text'])else:st.warning("Please enter a prompt to generate text.")# Run the Streamlit appif __name__ == '__main__':st.run()
app.py+27-52