import streamlit as st
import requests
import os

st.title("Ask Codestral-22B-v0.1")

hf_token = os.getenv("HUGGINGFACE_HUB_TOKEN")
st.write(hf_token)
api_url = "https://api-inference.huggingface.co/models/mistralai/Codestral-22B-v0.1"  # Update with correct owner and model name

headers = {
    "Authorization": f"Bearer {hf_token}"
}

question = st.text_input("Enter your question:")
submit_button = st.button("Submit")

if submit_button and question:
    with st.spinner("Generating answer..."):
        try:
            response = requests.post(
                api_url,
                headers=headers,
                json={"inputs": question}
            )
            response.raise_for_status()
            result = response.json()
            answer = result[0]['generated_text']
            st.text_area("Answer", value=answer, height=200)
        except requests.exceptions.RequestException as e:
            st.error(f"Error calling the API: {e}")
        except KeyError:
            st.error("Error processing the API response.")