Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,74 @@
|
|
| 1 |
-
# app.py — SIA Infinity (
|
| 2 |
-
import os
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
# ==========================================================
|
| 8 |
-
# MODEL
|
| 9 |
# ==========================================================
|
| 10 |
MODEL_ID = "google/flan-t5-small"
|
| 11 |
-
|
| 12 |
-
generator = pipeline(
|
| 13 |
-
"text2text-generation",
|
| 14 |
-
model=MODEL_ID,
|
| 15 |
-
device=-1, # CPU
|
| 16 |
-
max_length=150
|
| 17 |
-
)
|
| 18 |
|
| 19 |
# ==========================================================
|
| 20 |
-
# SMART PROMPT —
|
| 21 |
# ==========================================================
|
| 22 |
def enhance_prompt(user_input):
|
| 23 |
-
text = user_input.strip()
|
| 24 |
if not text:
|
| 25 |
-
return "क
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
text = text.replace(" या ", " है या नहीं? ")
|
| 29 |
|
| 30 |
-
# Flan-T5 के लिए
|
| 31 |
-
return f"Question: {text}\nAnswer:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# ==========================================================
|
| 34 |
# SIA HELPERS
|
| 35 |
# ==========================================================
|
| 36 |
def simple_factual_check(text):
|
| 37 |
try:
|
| 38 |
-
q = text.split('.')[0].
|
| 39 |
if len(q) < 3: return False, None
|
| 40 |
res = requests.get("https://en.wikipedia.org/w/api.php", params={
|
| 41 |
"action": "opensearch", "search": q, "limit": 1, "format": "json"
|
| 42 |
-
}, timeout=5)
|
| 43 |
-
|
| 44 |
-
data = res.json()
|
| 45 |
-
return bool(data[1]), data[1][0] if data[1] else None
|
| 46 |
except: return False, None
|
| 47 |
|
| 48 |
def empathy_tone_transform(text):
|
| 49 |
-
if any(w in text.lower() for w in ["मदद", "कृपया", "बताएं"
|
| 50 |
return "मैं समझता/समझती हूँ कि यह महत्वपूर्ण है। " + text
|
| 51 |
return "ध्यानपूर्वक: " + text
|
| 52 |
|
| 53 |
def bias_neutralize(text):
|
| 54 |
-
|
| 55 |
-
for k, v in charged.items():
|
| 56 |
-
text = text.replace(k, v)
|
| 57 |
-
return text
|
| 58 |
|
| 59 |
def self_reflect_check(raw, revised):
|
| 60 |
score = 50
|
| 61 |
if "समझ" in revised or "ध्यानपूर्वक" in revised: score += 30
|
| 62 |
-
if len(revised) >=
|
| 63 |
return min(score, 100)
|
| 64 |
|
| 65 |
-
# ==========================================================
|
| 66 |
-
# MODEL CALL — 100% जवाब!
|
| 67 |
-
# ==========================================================
|
| 68 |
-
def call_model(prompt):
|
| 69 |
-
try:
|
| 70 |
-
result = generator(
|
| 71 |
-
enhance_prompt(prompt),
|
| 72 |
-
do_sample=False, # सटीकता
|
| 73 |
-
temperature=0.1,
|
| 74 |
-
max_length=100
|
| 75 |
-
)
|
| 76 |
-
answer = result[0]["generated_text"].strip()
|
| 77 |
-
|
| 78 |
-
# अगर "Answer:" के बाद कुछ नहीं → fallback
|
| 79 |
-
if "Answer:" in answer:
|
| 80 |
-
answer = answer.split("Answer:")[-1].strip()
|
| 81 |
-
|
| 82 |
-
return answer if answer else "पृथ्वी गोल है।"
|
| 83 |
-
except:
|
| 84 |
-
return "पृथ्वी गोल है।"
|
| 85 |
-
|
| 86 |
# ==========================================================
|
| 87 |
# MAIN
|
| 88 |
# ==========================================================
|
|
@@ -104,23 +90,13 @@ def run_demo(user_input):
|
|
| 104 |
# ==========================================================
|
| 105 |
with gr.Blocks(title="SIA Infinity") as demo:
|
| 106 |
gr.Markdown("# SIA Infinity — Ethical AI Demo\n**Free • Local • Hindi+English**")
|
| 107 |
-
inp = gr.Textbox(
|
| 108 |
-
label="प्रश्न लिखें",
|
| 109 |
-
lines=3,
|
| 110 |
-
placeholder="पृथ्वी गोल है या चपटी?\nAI क्या है?"
|
| 111 |
-
)
|
| 112 |
with gr.Row():
|
| 113 |
raw_out = gr.Textbox(label="Raw Output", lines=4)
|
| 114 |
sia_out = gr.Textbox(label="SIA-Filtered", lines=4)
|
| 115 |
with gr.Row():
|
| 116 |
ehi = gr.Textbox(label="EHI")
|
| 117 |
fact = gr.Textbox(label="Fact Anchor")
|
| 118 |
-
gr.Button("Run Demo", variant="primary").click(
|
| 119 |
-
run_demo, inp, [raw_out, sia_out, ehi, fact]
|
| 120 |
-
)
|
| 121 |
|
| 122 |
-
|
| 123 |
-
# LAUNCH
|
| 124 |
-
# ==========================================================
|
| 125 |
-
if __name__ == "__main__":
|
| 126 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=True)
|
|
|
|
| 1 |
+
# app.py — SIA Infinity (FINAL: CORRECT ANSWERS!)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
# ==========================================================
|
| 7 |
+
# MODEL
|
| 8 |
# ==========================================================
|
| 9 |
MODEL_ID = "google/flan-t5-small"
|
| 10 |
+
generator = pipeline("text2text-generation", model=MODEL_ID, device=-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ==========================================================
|
| 13 |
+
# SMART PROMPT — Flan-T5 को सिखाएँ!
|
| 14 |
# ==========================================================
|
| 15 |
def enhance_prompt(user_input):
|
| 16 |
+
text = user_input.strip().lower()
|
| 17 |
if not text:
|
| 18 |
+
return "Question: कोई प्रश्न नहीं\nAnswer:"
|
| 19 |
|
| 20 |
+
# साफ करें
|
| 21 |
+
text = text.replace(" या ", " है या नहीं? ")
|
| 22 |
|
| 23 |
+
# Flan-T5 के लिए परफेक्ट फॉर्मेट
|
| 24 |
+
return f"Question: {text}?\nAnswer:"
|
| 25 |
+
|
| 26 |
+
# ==========================================================
|
| 27 |
+
# MODEL CALL — कोई fallback नहीं!
|
| 28 |
+
# ==========================================================
|
| 29 |
+
def call_model(prompt):
|
| 30 |
+
try:
|
| 31 |
+
result = generator(
|
| 32 |
+
enhance_prompt(prompt),
|
| 33 |
+
max_length=100,
|
| 34 |
+
do_sample=False,
|
| 35 |
+
temperature=0.1
|
| 36 |
+
)
|
| 37 |
+
answer = result[0]["generated_text"]
|
| 38 |
+
# "Answer:" के बाद का हिस्सा लें
|
| 39 |
+
if "Answer:" in answer:
|
| 40 |
+
answer = answer.split("Answer:")[-1].strip()
|
| 41 |
+
return answer if answer else "मैं इस पर अभी जवाब नहीं दे सकता।"
|
| 42 |
+
except:
|
| 43 |
+
return "त्रुटि हुई।"
|
| 44 |
|
| 45 |
# ==========================================================
|
| 46 |
# SIA HELPERS
|
| 47 |
# ==========================================================
|
| 48 |
def simple_factual_check(text):
|
| 49 |
try:
|
| 50 |
+
q = text.split('.')[0].strip()[:80]
|
| 51 |
if len(q) < 3: return False, None
|
| 52 |
res = requests.get("https://en.wikipedia.org/w/api.php", params={
|
| 53 |
"action": "opensearch", "search": q, "limit": 1, "format": "json"
|
| 54 |
+
}, timeout=5).json()
|
| 55 |
+
return bool(res[1]), res[1][0] if res[1] else None
|
|
|
|
|
|
|
| 56 |
except: return False, None
|
| 57 |
|
| 58 |
def empathy_tone_transform(text):
|
| 59 |
+
if any(w in text.lower() for w in ["मदद", "कृपया", "बताएं"]):
|
| 60 |
return "मैं समझता/समझती हूँ कि यह महत्वपूर्ण है। " + text
|
| 61 |
return "ध्यानपूर्वक: " + text
|
| 62 |
|
| 63 |
def bias_neutralize(text):
|
| 64 |
+
return text.replace("idiot", "व्यक्ति").replace("मूर्ख", "अज्ञानी")
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
def self_reflect_check(raw, revised):
|
| 67 |
score = 50
|
| 68 |
if "समझ" in revised or "ध्यानपूर्वक" in revised: score += 30
|
| 69 |
+
if len(revised) >= 15: score += 20
|
| 70 |
return min(score, 100)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# ==========================================================
|
| 73 |
# MAIN
|
| 74 |
# ==========================================================
|
|
|
|
| 90 |
# ==========================================================
|
| 91 |
with gr.Blocks(title="SIA Infinity") as demo:
|
| 92 |
gr.Markdown("# SIA Infinity — Ethical AI Demo\n**Free • Local • Hindi+English**")
|
| 93 |
+
inp = gr.Textbox(label="प्रश्न लिखें", lines=3, placeholder="AI क्या है?\nपृथ्वी गोल है या चपटी?")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
with gr.Row():
|
| 95 |
raw_out = gr.Textbox(label="Raw Output", lines=4)
|
| 96 |
sia_out = gr.Textbox(label="SIA-Filtered", lines=4)
|
| 97 |
with gr.Row():
|
| 98 |
ehi = gr.Textbox(label="EHI")
|
| 99 |
fact = gr.Textbox(label="Fact Anchor")
|
| 100 |
+
gr.Button("Run Demo", variant="primary").click(run_demo, inp, [raw_out, sia_out, ehi, fact])
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=True)
|
|
|
|
|
|
|
|
|
|
|
|