Download app.py from RefugeeOpportunityNetwork/RON-Chatbot: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/spaces/RefugeeOpportunityNetwork/RON-Chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/RefugeeOpportunityNetwork/RON-Chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/RefugeeOpportunityNetwork/RON-Chatbot/resolve/main/app.py
2.01 kB
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| try: | |
| with open("RON.pdf", "rb") as f: | |
| import PyPDF2 | |
| reader = PyPDF2.PdfReader(f) | |
| pdf_text = "" | |
| for page in reader.pages: | |
| text = page.extract_text() | |
| if text: | |
| pdf_text += text + "\n" | |
| except: | |
| pdf_text = "PDF not found" | |
| client = InferenceClient(api_key="hf_default") | |
| def chat_with_ai(message, history): | |
| if "PDF not found" in pdf_text: | |
| return "PDF file not found" | |
| # Build context from PDF | |
| context = f"You are a helpful refugee integration assistant. Use this information to answer questions:\n\n{pdf_text[:2000]}\n\n" | |
| # Build conversation history | |
| messages = [{"role": "system", "content": context}] | |
| for msg, response in (history or []): | |
| messages.append({"role": "user", "content": msg}) | |
| messages.append({"role": "assistant", "content": response}) | |
| messages.append({"role": "user", "content": message}) | |
| # Get AI response | |
| try: | |
| response = client.chat_completion( | |
| model="mistralai/Mistral-7B-Instruct-v0.1", | |
| messages=messages, | |
| max_tokens=500 | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"AI response error: {str(e)[:100]}" | |
| with gr.Blocks() as app: | |
| gr.Markdown("# 🇳🇱 Refugee Integration AI Chatbot") | |
| gr.Markdown("Chat naturally - powered by your PDF data") | |
| chatbot = gr.Chatbot(type="messages", height=500) | |
| msg = gr.Textbox( | |
| label="Your Question", | |
| placeholder="Ask about housing, jobs, language, education, health..." | |
| ) | |
| send = gr.Button("Send") | |
| def respond(message, chat_history): | |
| response = chat_with_ai(message, chat_history) | |
| return "", chat_history + [(message, response)] | |
| send.click(respond, [msg, chatbot], [msg, chatbot]) | |
| msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
| app.launch() | |