Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +114 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
# -------------------------
|
| 6 |
+
# Load Model & Tokenizer
|
| 7 |
+
# -------------------------
|
| 8 |
+
model_name = "ibm-granite/granite-3.2-2b-instruct"
|
| 9 |
+
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
model_name,
|
| 13 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 14 |
+
device_map="auto" if torch.cuda.is_available() else None,
|
| 15 |
+
low_cpu_mem_usage=True
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
if tokenizer.pad_token is None:
|
| 19 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 20 |
+
|
| 21 |
+
# -------------------------
|
| 22 |
+
# Response Generator
|
| 23 |
+
# -------------------------
|
| 24 |
+
def generate_response(prompt, max_length=1024):
|
| 25 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 26 |
+
if torch.cuda.is_available():
|
| 27 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model.generate(
|
| 30 |
+
**inputs,
|
| 31 |
+
max_length=max_length,
|
| 32 |
+
temperature=0.7,
|
| 33 |
+
do_sample=True,
|
| 34 |
+
pad_token_id=tokenizer.eos_token_id
|
| 35 |
+
)
|
| 36 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 37 |
+
if response.startswith(prompt):
|
| 38 |
+
response = response[len(prompt):].strip()
|
| 39 |
+
return response
|
| 40 |
+
|
| 41 |
+
# -------------------------
|
| 42 |
+
# Task 1: City Analysis
|
| 43 |
+
# -------------------------
|
| 44 |
+
def city_analysis(city_name):
|
| 45 |
+
prompt = f"""
|
| 46 |
+
Provide a detailed analysis of {city_name} including:
|
| 47 |
+
1. Crime Index and safety statistics
|
| 48 |
+
2. Accident rates and traffic safety information
|
| 49 |
+
3. Overall safety assessment
|
| 50 |
+
|
| 51 |
+
City: {city_name}
|
| 52 |
+
Analysis:
|
| 53 |
+
"""
|
| 54 |
+
return generate_response(prompt, max_length=1000)
|
| 55 |
+
|
| 56 |
+
# -------------------------
|
| 57 |
+
# Task 2: Citizen Interaction
|
| 58 |
+
# -------------------------
|
| 59 |
+
def citizen_interaction(query):
|
| 60 |
+
prompt = f"""
|
| 61 |
+
As a government assistant, provide accurate and helpful information about the following citizen query related to public services, government policies, or civic issues:
|
| 62 |
+
|
| 63 |
+
Query: {query}
|
| 64 |
+
Response:
|
| 65 |
+
"""
|
| 66 |
+
return generate_response(prompt, max_length=1000)
|
| 67 |
+
|
| 68 |
+
# -------------------------
|
| 69 |
+
# Login Function
|
| 70 |
+
# -------------------------
|
| 71 |
+
def login_user(name, city, mobile):
|
| 72 |
+
if not name or not city or not mobile:
|
| 73 |
+
return gr.update(visible=True), gr.update(visible=False), "⚠️ Please fill all details!"
|
| 74 |
+
else:
|
| 75 |
+
welcome_msg = f"✅ Welcome {name} from {city}! (Mobile: {mobile})"
|
| 76 |
+
return gr.update(visible=False), gr.update(visible=True), welcome_msg
|
| 77 |
+
|
| 78 |
+
# -------------------------
|
| 79 |
+
# Gradio UI with Login
|
| 80 |
+
# -------------------------
|
| 81 |
+
with gr.Blocks() as app:
|
| 82 |
+
gr.Markdown("# 🔐 Citizen-AI Login Page")
|
| 83 |
+
|
| 84 |
+
# --- Login Page ---
|
| 85 |
+
with gr.Group(visible=True) as login_page:
|
| 86 |
+
name_in = gr.Textbox(label="Name", placeholder="Enter your name")
|
| 87 |
+
city_in = gr.Textbox(label="City", placeholder="Enter your city")
|
| 88 |
+
mobile_in = gr.Textbox(label="Mobile Number", placeholder="Enter your mobile number")
|
| 89 |
+
login_btn = gr.Button("Login")
|
| 90 |
+
login_status = gr.Markdown("")
|
| 91 |
+
|
| 92 |
+
# --- Main App Page (Initially Hidden) ---
|
| 93 |
+
with gr.Group(visible=False) as main_page:
|
| 94 |
+
gr.Markdown("## 🏙️ Citizen-AI: City Analysis & Public Services Assistant")
|
| 95 |
+
|
| 96 |
+
with gr.Tabs():
|
| 97 |
+
with gr.TabItem("City Analysis"):
|
| 98 |
+
city_input = gr.Textbox(label="Enter City Name", placeholder="e.g., New York, London, Mumbai...")
|
| 99 |
+
city_output = gr.Textbox(label="City Analysis Result", lines=15)
|
| 100 |
+
gr.Button("Analyze City").click(city_analysis, inputs=city_input, outputs=city_output)
|
| 101 |
+
|
| 102 |
+
with gr.TabItem("Citizen Services"):
|
| 103 |
+
citizen_query = gr.Textbox(label="Your Query", placeholder="Ask about public services, government policies, civic issues...", lines=4)
|
| 104 |
+
citizen_output = gr.Textbox(label="Government Response", lines=15)
|
| 105 |
+
gr.Button("Get Info").click(citizen_interaction, inputs=citizen_query, outputs=citizen_output)
|
| 106 |
+
|
| 107 |
+
# Button Action → Switch Pages
|
| 108 |
+
login_btn.click(
|
| 109 |
+
fn=login_user,
|
| 110 |
+
inputs=[name_in, city_in, mobile_in],
|
| 111 |
+
outputs=[login_page, main_page, login_status]
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
gradio
|