oberbics commited on
Commit
f06ced6
·
verified ·
1 Parent(s): 69ab256

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -39
app.py CHANGED
@@ -1,14 +1,11 @@
1
- # app.py - Fixed for Hugging Face Spaces with lazy loading
2
- import spaces
3
  import gradio as gr
4
  import torch
5
- from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
6
  import re
 
7
 
8
  # Model configuration
9
  MODEL_ID = "oberbics/llama-3.1-model-newspaper-arguments-V1"
10
 
11
- # System prompt
12
  SYSTEM_PROMPT = """You are an expert at analyzing German historical texts.
13
 
14
  OUTPUT FORMAT - EXACTLY these 4 XML tags and NOTHING else:
@@ -22,7 +19,6 @@ RULES:
22
  - If no argument exists, use NA for all fields
23
  - Extract complete argumentative passages, not fragments"""
24
 
25
- # Initialize model and tokenizer at module level (like the working example)
26
  print("Loading tokenizer...")
27
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
28
  tokenizer.pad_token = tokenizer.eos_token
@@ -44,14 +40,10 @@ model = AutoModelForCausalLM.from_pretrained(
44
  )
45
  print("Model loaded successfully!")
46
 
47
- @spaces.GPU(duration=120) # Request GPU for 120 seconds per inference
48
  def extract_arguments(text, temperature=0.1):
49
- """Extract argumentative units from text"""
50
-
51
  if not text or not text.strip():
52
  return "", "Please enter some text to analyze."
53
 
54
- # Build prompt
55
  prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
56
  {SYSTEM_PROMPT}<|eot_id|>
57
  <|start_header_id|>user<|end_header_id|>
@@ -59,9 +51,7 @@ Extract arguments from this German historical text:
59
  {text}<|eot_id|>
60
  <|start_header_id|>assistant<|end_header_id|>"""
61
 
62
- # Tokenize and generate
63
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048).to(model.device)
64
-
65
  input_length = inputs['input_ids'].shape[1]
66
 
67
  with torch.no_grad():
@@ -75,24 +65,19 @@ Extract arguments from this German historical text:
75
  repetition_penalty=1.1
76
  )
77
 
78
- # Decode response
79
  generated_tokens = outputs[0][input_length:]
80
  response = tokenizer.decode(generated_tokens, skip_special_tokens=True)
81
-
82
- # Ensure proper XML structure
83
  if not response.startswith('<argument>'):
84
  arg_start = response.find('<argument>')
85
  if arg_start != -1:
86
  response = response[arg_start:]
87
 
88
- # Parse and format
89
  formatted = format_output(response)
90
-
91
  return response, formatted
92
 
93
  def format_output(xml_response):
94
- """Format XML response for display"""
95
-
96
  def extract_field(field_name):
97
  pattern = f'<{field_name}>(.*?)</{field_name}>'
98
  match = re.search(pattern, xml_response, re.DOTALL)
@@ -103,10 +88,8 @@ def format_output(xml_response):
103
  explanation = extract_field('explanation')
104
  verification = extract_field('human_verification_needed')
105
 
106
- has_argument = argument != 'NA' and argument != 'ERROR'
107
-
108
- if has_argument:
109
- result = f"""✅ **Argument Found**
110
 
111
  **Argument:** {argument}
112
 
@@ -116,28 +99,16 @@ def format_output(xml_response):
116
 
117
  **Verification Needed:** {verification}"""
118
  else:
119
- result = """❌ **No Argument Found**
120
 
121
  The text does not contain an argumentative unit."""
122
-
123
- return result
124
 
125
- # Create simple interface (following the working example pattern)
126
  demo = gr.Interface(
127
  fn=extract_arguments,
128
  inputs=[
129
- gr.Textbox(
130
- label="Input Text",
131
- placeholder="Enter German historical newspaper text here...",
132
- lines=10
133
- ),
134
- gr.Slider(
135
- minimum=0.01,
136
- maximum=0.3,
137
- value=0.1,
138
- step=0.01,
139
- label="Temperature (lower = more consistent)"
140
- )
141
  ],
142
  outputs=[
143
  gr.Textbox(label="Raw XML Output", lines=8),
@@ -153,4 +124,4 @@ demo = gr.Interface(
153
  )
154
 
155
  if __name__ == "__main__":
156
- demo.launch()
 
 
 
1
  import gradio as gr
2
  import torch
 
3
  import re
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
5
 
6
  # Model configuration
7
  MODEL_ID = "oberbics/llama-3.1-model-newspaper-arguments-V1"
8
 
 
9
  SYSTEM_PROMPT = """You are an expert at analyzing German historical texts.
10
 
11
  OUTPUT FORMAT - EXACTLY these 4 XML tags and NOTHING else:
 
19
  - If no argument exists, use NA for all fields
20
  - Extract complete argumentative passages, not fragments"""
21
 
 
22
  print("Loading tokenizer...")
23
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
24
  tokenizer.pad_token = tokenizer.eos_token
 
40
  )
41
  print("Model loaded successfully!")
42
 
 
43
  def extract_arguments(text, temperature=0.1):
 
 
44
  if not text or not text.strip():
45
  return "", "Please enter some text to analyze."
46
 
 
47
  prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
48
  {SYSTEM_PROMPT}<|eot_id|>
49
  <|start_header_id|>user<|end_header_id|>
 
51
  {text}<|eot_id|>
52
  <|start_header_id|>assistant<|end_header_id|>"""
53
 
 
54
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048).to(model.device)
 
55
  input_length = inputs['input_ids'].shape[1]
56
 
57
  with torch.no_grad():
 
65
  repetition_penalty=1.1
66
  )
67
 
 
68
  generated_tokens = outputs[0][input_length:]
69
  response = tokenizer.decode(generated_tokens, skip_special_tokens=True)
70
+
71
+ # Fix XML start
72
  if not response.startswith('<argument>'):
73
  arg_start = response.find('<argument>')
74
  if arg_start != -1:
75
  response = response[arg_start:]
76
 
 
77
  formatted = format_output(response)
 
78
  return response, formatted
79
 
80
  def format_output(xml_response):
 
 
81
  def extract_field(field_name):
82
  pattern = f'<{field_name}>(.*?)</{field_name}>'
83
  match = re.search(pattern, xml_response, re.DOTALL)
 
88
  explanation = extract_field('explanation')
89
  verification = extract_field('human_verification_needed')
90
 
91
+ if argument != 'NA' and argument != 'ERROR':
92
+ return f"""✅ **Argument Found**
 
 
93
 
94
  **Argument:** {argument}
95
 
 
99
 
100
  **Verification Needed:** {verification}"""
101
  else:
102
+ return """❌ **No Argument Found**
103
 
104
  The text does not contain an argumentative unit."""
 
 
105
 
106
+ # Gradio interface
107
  demo = gr.Interface(
108
  fn=extract_arguments,
109
  inputs=[
110
+ gr.Textbox(label="Input Text", placeholder="Enter German historical newspaper text here...", lines=10),
111
+ gr.Slider(minimum=0.01, maximum=0.3, value=0.1, step=0.01, label="Temperature (lower = more consistent)")
 
 
 
 
 
 
 
 
 
 
112
  ],
113
  outputs=[
114
  gr.Textbox(label="Raw XML Output", lines=8),
 
124
  )
125
 
126
  if __name__ == "__main__":
127
+ demo.launch()