CoolFace
Apppublic

SwapnilPandey/Grit_Crew_Bot

sourceHugging Faceotherupdated 1y agoView on Hugging Face
0likes
app.py45 linesDownload Raw Back to root
1import os2import gradio as gr3from google import genai4 5# Get API key from HF Space secret6API_KEY = os.environ.get("GEMINI_API_KEY")7if not API_KEY:8    raise RuntimeError("Set GEMINI_API_KEY in your Hugging Face Space secrets!")9 10client = genai.Client(api_key=API_KEY)11 12# Upload SOP at startup13SOP_PATH = "SOP.pdf"14if not os.path.exists(SOP_PATH):15    raise RuntimeError("SOP.pdf not found in Space files!")16 17print("Uploading SOP.pdf to Gemini...")18with open(SOP_PATH, "rb") as f:19    sop_file = client.files.upload(file=f, config={"mime_type": "application/pdf"})20print("Upload finished.")21 22def answer_question(question: str):23    if not question.strip():24        return "Please ask a question about the SOP."25    prompt = [26        sop_file,27        f"Answer this question based only on the SOP: {question}"28    ]29    resp = client.models.generate_content(30        model="gemini-2.5-flash",31        contents=prompt32    )33    return getattr(resp, "text", str(resp))34 35iface = gr.Interface(36    fn=answer_question,37    inputs="text",38    outputs="text",39    title="๐Ÿ“˜ Grit Crew Bot",40    description="Ask any question about the SOP document."41)42 43if __name__ == "__main__":44    iface.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))45