Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,120 +2,97 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
mistral_model = None
|
| 13 |
-
mistral_tokenizer = None
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
llama_tokenizer = AutoTokenizer.from_pretrained(
|
| 21 |
-
"meta-llama/Meta-Llama-3-8B-Instruct", token=HF_TOKEN
|
| 22 |
-
)
|
| 23 |
-
llama_model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
-
"meta-llama/Meta-Llama-3-8B-Instruct",
|
| 25 |
-
torch_dtype=torch.float16,
|
| 26 |
-
device_map="auto",
|
| 27 |
-
token=HF_TOKEN
|
| 28 |
-
)
|
| 29 |
-
pipe = pipeline("text-generation", model=llama_model, tokenizer=llama_tokenizer)
|
| 30 |
-
result = pipe(prompt, max_new_tokens=350, temperature=0.7)
|
| 31 |
-
return result[0]["generated_text"].strip()
|
| 32 |
-
except Exception as e:
|
| 33 |
-
return f"⚠️ LLaMA failed: {str(e)}\nSwitching to Mistral...\n\n" + call_mistral(prompt)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
return
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
if not
|
| 57 |
-
return "
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
2. Who it's for
|
| 67 |
3. Deadline (if known)
|
| 68 |
-
4.
|
| 69 |
-
|
| 70 |
-
return "AI Recommendation:\n\n" + call_llama(prompt)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
primary_hue="orange",
|
| 75 |
-
secondary_hue="
|
| 76 |
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 77 |
-
radius_size=gr.themes.sizes.
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
# ---------- UI ELEMENTS ----------
|
| 81 |
-
region_dropdown = gr.Dropdown(
|
| 82 |
-
choices=["Alberta", "British Columbia", "Manitoba", "Nunavut", "Ontario", "Quebec", "Saskatchewan", "Yukon"],
|
| 83 |
-
label="Select your region"
|
| 84 |
)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
label="Type of project"
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
focus_dropdown = gr.Dropdown(
|
| 92 |
-
choices=["Youth", "Elders", "Women", "Two-Spirit", "Entrepreneurs", "Community-wide"],
|
| 93 |
-
label="Community focus"
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
output_box = gr.Textbox(label="Suggested Grants", lines=8)
|
| 97 |
-
|
| 98 |
-
# ---------- INTERFACE ----------
|
| 99 |
-
iface = gr.Interface(
|
| 100 |
fn=find_funding,
|
| 101 |
-
inputs=
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
)
|
| 108 |
|
| 109 |
-
# ---------- FOOTER ----------
|
| 110 |
-
footer_html = """## ✨ Why This Matters
|
| 111 |
-
This tool shows how AI can help Indigenous communities **access resources** and **reclaim agency**.
|
| 112 |
-
All AI suggestions should be **verified by the community** and used **with consent and cultural care**.
|
| 113 |
-
Created with ❤️ by Shingai Manjengwa, @tjido"""
|
| 114 |
-
|
| 115 |
-
demo = gr.Blocks(theme=custom_theme, fill_height=True)
|
| 116 |
-
with demo:
|
| 117 |
-
iface.render()
|
| 118 |
-
gr.Markdown(footer_html)
|
| 119 |
-
|
| 120 |
if __name__ == "__main__":
|
| 121 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 5 |
+
from langchain.tools.tavily_search import TavilySearchResults
|
| 6 |
|
| 7 |
+
# Load environment variables
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
+
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
| 10 |
|
| 11 |
+
# Check API key
|
| 12 |
+
if not TAVILY_API_KEY:
|
| 13 |
+
raise ValueError("Missing TAVILY_API_KEY in environment variables")
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Load Mistral model
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 17 |
+
"mistralai/Mistral-7B-Instruct-v0.1",
|
| 18 |
+
token=HF_TOKEN
|
| 19 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
+
"mistralai/Mistral-7B-Instruct-v0.1",
|
| 23 |
+
torch_dtype=torch.float16,
|
| 24 |
+
device_map="auto",
|
| 25 |
+
token=HF_TOKEN
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
llm = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 29 |
+
|
| 30 |
+
# Tavily Search
|
| 31 |
+
search = TavilySearchResults(api_key=TAVILY_API_KEY)
|
| 32 |
+
|
| 33 |
+
# Main function
|
| 34 |
+
def find_funding(project_description):
|
| 35 |
+
if not project_description or len(project_description.strip()) < 5:
|
| 36 |
+
return "Please describe your project to find funding opportunities."
|
| 37 |
+
|
| 38 |
+
# Live search on trusted sites
|
| 39 |
+
query = f"site:canada.ca OR site:canadacouncil.ca grants for {project_description}"
|
| 40 |
+
search_results = search.run(query)
|
| 41 |
+
|
| 42 |
+
if not search_results:
|
| 43 |
+
return "No search results found. Try a different project description."
|
| 44 |
+
|
| 45 |
+
# Format results for prompt
|
| 46 |
+
result_snippets = "\n".join([f"- {r['title']} | {r['url']}" for r in search_results[:5]])
|
| 47 |
+
|
| 48 |
+
prompt = f"""<s>[INST]
|
| 49 |
+
You are a Funding Advisor for Indigenous communities in Canada. Based on the following search results and the user's project description, return 3 to 5 funding opportunities.
|
| 50 |
+
|
| 51 |
+
User Project Description:
|
| 52 |
+
"{project_description}"
|
| 53 |
+
|
| 54 |
+
Search Results:
|
| 55 |
+
{result_snippets}
|
| 56 |
+
|
| 57 |
+
For each grant, include:
|
| 58 |
+
1. Grant name
|
| 59 |
2. Who it's for
|
| 60 |
3. Deadline (if known)
|
| 61 |
+
4. Link
|
| 62 |
+
[/INST]"""
|
|
|
|
| 63 |
|
| 64 |
+
try:
|
| 65 |
+
result = llm(prompt, max_new_tokens=500, temperature=0.7)[0]["generated_text"]
|
| 66 |
+
return result.strip()
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return f"⚠️ AI model error: {str(e)}"
|
| 69 |
+
|
| 70 |
+
# Theme
|
| 71 |
+
theme = gr.themes.Soft(
|
| 72 |
primary_hue="orange",
|
| 73 |
+
secondary_hue="green",
|
| 74 |
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 75 |
+
radius_size=gr.themes.sizes.radius_md,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
+
# UI
|
| 79 |
+
demo = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
fn=find_funding,
|
| 81 |
+
inputs=gr.Textbox(
|
| 82 |
+
lines=3,
|
| 83 |
+
label="Describe your project",
|
| 84 |
+
placeholder="e.g. Language revitalization course for Dene youth in Yukon"
|
| 85 |
+
),
|
| 86 |
+
outputs=gr.Textbox(
|
| 87 |
+
label="Funding Opportunities",
|
| 88 |
+
lines=10,
|
| 89 |
+
show_copy_button=True
|
| 90 |
+
),
|
| 91 |
+
title="💸 Indigenous Project Funding Finder",
|
| 92 |
+
description="Enter a short project description. This tool searches trusted sites and uses AI to summarize relevant funding programs.",
|
| 93 |
+
theme=theme,
|
| 94 |
+
allow_flagging="never"
|
| 95 |
)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
if __name__ == "__main__":
|
| 98 |
demo.launch()
|