Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
import json
|
5 |
+
|
6 |
+
# Load the ConspEmoLLM model
|
7 |
+
MODEL_PATH = "lzw1008/ConspEmoLLM-v2"
|
8 |
+
print("Loading ConspEmoLLM...")
|
9 |
+
|
10 |
+
try:
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_PATH, device_map='auto')
|
13 |
+
print("Model loaded successfully!")
|
14 |
+
model_loaded = True
|
15 |
+
except Exception as e:
|
16 |
+
print(f"Error: {e}")
|
17 |
+
tokenizer = None
|
18 |
+
model = None
|
19 |
+
model_loaded = False
|
20 |
+
|
21 |
+
def analyze_text(text):
|
22 |
+
"""Analyze text with ConspEmoLLM"""
|
23 |
+
if not model_loaded:
|
24 |
+
return {"error": "Model not loaded", "status": "error"}
|
25 |
+
|
26 |
+
if not text.strip():
|
27 |
+
return {"error": "No text provided", "status": "error"}
|
28 |
+
|
29 |
+
try:
|
30 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
31 |
+
|
32 |
+
with torch.no_grad():
|
33 |
+
outputs = model.generate(
|
34 |
+
inputs["input_ids"],
|
35 |
+
max_new_tokens=100,
|
36 |
+
temperature=0.7,
|
37 |
+
do_sample=True,
|
38 |
+
pad_token_id=tokenizer.eos_token_id
|
39 |
+
)
|
40 |
+
|
41 |
+
result = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
42 |
+
|
43 |
+
return {
|
44 |
+
"result": result,
|
45 |
+
"original_text": text,
|
46 |
+
"status": "success"
|
47 |
+
}
|
48 |
+
|
49 |
+
except Exception as e:
|
50 |
+
return {"error": str(e), "status": "error"}
|
51 |
+
|
52 |
+
def api_interface(text):
|
53 |
+
"""Main interface for API calls"""
|
54 |
+
if text.strip().lower() == "/health":
|
55 |
+
return json.dumps({"status": "healthy", "model_loaded": model_loaded}, indent=2)
|
56 |
+
|
57 |
+
result = analyze_text(text)
|
58 |
+
return json.dumps(result, indent=2)
|
59 |
+
|
60 |
+
# Create simple Gradio interface
|
61 |
+
demo = gr.Interface(
|
62 |
+
fn=api_interface,
|
63 |
+
inputs=gr.Textbox(
|
64 |
+
lines=5,
|
65 |
+
placeholder="Enter text to analyze...\nOr type '/health' to check status",
|
66 |
+
label="Input Text"
|
67 |
+
),
|
68 |
+
outputs=gr.Textbox(
|
69 |
+
lines=10,
|
70 |
+
label="Analysis Result (JSON)"
|
71 |
+
),
|
72 |
+
title="ConspEmoLLM API",
|
73 |
+
description="Analyze text for conspiracy theories and emotions",
|
74 |
+
examples=[
|
75 |
+
["/health"],
|
76 |
+
["I think the government is hiding vaccine information and this makes me worried."],
|
77 |
+
["The media manipulates climate data to control us and I feel angry about it."]
|
78 |
+
]
|
79 |
+
)
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
demo.launch()
|