Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load lightweight model
|
| 5 |
+
philo_model = pipeline("text-generation", model="gpt2")
|
| 6 |
+
|
| 7 |
+
def analyze_text(text):
|
| 8 |
+
if not text.strip():
|
| 9 |
+
return "Please enter some text!"
|
| 10 |
+
|
| 11 |
+
# Detect philosophical topic
|
| 12 |
+
keywords = ["philosophy", "ethics", "kant", "nietzsche", "existentialism",
|
| 13 |
+
"metaphysics", "epistemology", "ontology", "stoicism", "rationalism"]
|
| 14 |
+
is_philo = any(kw in text.lower() for kw in keywords)
|
| 15 |
+
|
| 16 |
+
if is_philo:
|
| 17 |
+
response = philo_model(
|
| 18 |
+
f"Discuss this philosophical concept in detail: {text}",
|
| 19 |
+
max_length=200,
|
| 20 |
+
do_sample=True,
|
| 21 |
+
temperature=0.7,
|
| 22 |
+
top_p=0.9
|
| 23 |
+
)
|
| 24 |
+
return response[0]["generated_text"]
|
| 25 |
+
else:
|
| 26 |
+
return "This text doesn't appear to be philosophical. Please enter a philosophical text."
|
| 27 |
+
|
| 28 |
+
# Gradio interface
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=analyze_text,
|
| 31 |
+
inputs=gr.Textbox(label="Enter philosophical text (English only)"),
|
| 32 |
+
outputs=gr.Textbox(label="Philosophical Analysis"),
|
| 33 |
+
title="🤖 Philsof - Philosophy Analyzer",
|
| 34 |
+
description="Analyze and discuss philosophical concepts using AI",
|
| 35 |
+
examples=[
|
| 36 |
+
["What is Kant's categorical imperative?"],
|
| 37 |
+
["Explain Nietzsche's concept of Ubermensch"],
|
| 38 |
+
["Compare rationalism and empiricism"]
|
| 39 |
+
],
|
| 40 |
+
allow_flagging="never"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
demo.launch()
|