Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files2 unmodified lines345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273742 unmodified linesimport streamlit as stfrom huggingface_hub import HfApi, InferenceClientst.set_page_config(page_title="HF Spaces Auth Debug", page_icon="π§")st.header("HF Spaces Auth Debug")# 1) Read + sanitize token from env (Repository secrets)HF_TOKEN = (os.getenv("HUGGINGFACEHUB_API_TOKEN") or "").strip().strip('"').strip("'")# Masked debug (won't leak token)st.write("Token present:", bool(HF_TOKEN), "| length:", len(HF_TOKEN) if HF_TOKEN else 0)if not HF_TOKEN:st.error("HUGGINGFACEHUB_API_TOKEN is missing/empty. Set it in Settings β Repository secrets, then Restart the Space.")st.stop()if not HF_TOKEN.startswith("hf_"):st.error("Token looks invalid (should start with 'hf_'). Recreate a User Access Token with at least 'read' scope.")st.stop()# Export for libs that read env implicitlyos.environ["HUGGINGFACEHUB_API_TOKEN"] = HF_TOKEN# 2) Prove auth against the Hubtry:me = HfApi(token=HF_TOKEN).whoami()st.success(f"Auth OK. Logged in as: {me.get('name') or me.get('email') or 'unknown'}")except Exception as e:st.exception(e)st.error("whoami() failed β bad token or missing scope. Fix token in Secrets and Restart.")st.stop()# 3) Direct serverless test (no LangChain)MODEL_TXT = "tiiuae/falcon-7b-instruct" # NOTE: lowercaseclient = InferenceClient(token=HF_TOKEN)try:txt = client.text_generation(model=MODEL_TXT,prompt="Say 'hello' once.",max_new_tokens=16,provider="hf-inference", # force serverless provider)st.success("Direct InferenceClient text_generation: OK")st.code(txt)except Exception as e:st.error("Direct InferenceClient failed (this is NOT a LangChain issue).")st.exception(e)# 4) LangChain wrapper testtry:from langchain_community.llms import HuggingFaceEndpointllm = HuggingFaceEndpoint(repo_id=MODEL_TXT,huggingfacehub_api_token=HF_TOKEN,# temperature=0.2, max_new_tokens=64,)answer = llm.invoke("What is the currency of India?")st.success("LangChain HuggingFaceEndpoint: OK")st.code(answer)except Exception as e:st.error("LangChain HuggingFaceEndpoint failed (auth validation inside LC).")st.exception(e)# Simple UIq = st.text_input("Ask me:")if st.button("Generate") and q.strip():try:out = client.text_generation(model=MODEL_TXT, prompt=q, max_new_tokens=128, provider="hf-inference")st.subheader("Answer:")st.write(out)except Exception as e:st.error(f"Generation failed: {e}")2 unmodified lines34567891011121314151617181920212223242526272829303132333435362 unmodified linesimport streamlit as stfrom huggingface_hub import HfApi, InferenceClientst.set_page_config(page_title="Q&A Demo", page_icon=":robot:")st.header("Q&A Demo")HF_TOKEN = (os.getenv("HUGGINGFACEHUB_API_TOKEN") or "").strip().strip('"').strip("'")if not HF_TOKEN or not HF_TOKEN.startswith("hf_"):st.error("Fix HUGGINGFACEHUB_API_TOKEN in Settings β Repository secrets, then Restart this Space.")st.stop()# sanity checkHfApi(token=HF_TOKEN).whoami()# β create client with model (no provider kw in 0.23.4)MODEL_ID = "tiiuae/falcon-7b-instruct"client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)def load_answer(question: str) -> str:return client.text_generation(prompt=question,max_new_tokens=128,# do_sample=True, temperature=0.2, top_p=0.9, repetition_penalty=1.1,return_full_text=False,)q = st.text_input("You:")if st.button("Generate") and q.strip():with st.spinner("Thinking..."):try:st.subheader("Answer:")st.write(load_answer(q))except Exception as e:st.error(f"Generation failed: {e}")
src/streamlit_app.py+22-60