oberbics commited on
Commit
3526b64
·
verified ·
1 Parent(s): 798966d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -28
app.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  import gradio as gr
2
  import torch
3
  import re
@@ -56,6 +59,34 @@ model = AutoModelForCausalLM.from_pretrained(
56
  )
57
  print("Model loaded successfully!")
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  @spaces.GPU
60
  def extract_arguments(text, temperature=0.1):
61
  if not text or not text.strip():
@@ -82,6 +113,7 @@ Extract arguments from historical text. Arguments in historical texts are often
82
  temperature = 0.3
83
  except:
84
  temperature = 0.1
 
85
  with torch.no_grad():
86
  outputs = model.generate(
87
  **inputs,
@@ -96,8 +128,6 @@ Extract arguments from historical text. Arguments in historical texts are often
96
  generated_tokens = outputs[0][input_length:]
97
  response = tokenizer.decode(generated_tokens, skip_special_tokens=True)
98
 
99
-
100
-
101
  # Fix XML start
102
  if not response.startswith('<argument>'):
103
  arg_start = response.find('<argument>')
@@ -107,31 +137,6 @@ Extract arguments from historical text. Arguments in historical texts are often
107
  formatted = format_output(response)
108
  return response, formatted
109
 
110
- '''def format_output(xml_response):
111
- def extract_field(field_name):
112
- pattern = f'<{field_name}>(.*?)</{field_name}>'
113
- match = re.search(pattern, xml_response, re.DOTALL)
114
- return match.group(1).strip() if match else 'ERROR'
115
-
116
- argument = extract_field('argument')
117
- claim = extract_field('claim')
118
- explanation = extract_field('explanation')
119
- verification = extract_field('human_verification_needed')
120
-
121
- if argument != 'NA' and argument != 'ERROR':
122
- return f"""✅ **Argument Found**
123
-
124
- **Argument:** {argument}
125
-
126
- **Claim:** {claim}
127
-
128
- **Explanation:** {explanation}
129
-
130
- **Verification Needed:** {verification}"""
131
- else:
132
- return """❌ **No Argument Found**
133
-
134
- The text does not contain an argumentative unit."""'''
135
 
136
  # Gradio interface
137
  demo = gr.Interface(
@@ -153,6 +158,5 @@ demo = gr.Interface(
153
  ]
154
  )
155
 
156
-
157
  if __name__ == "__main__":
158
  demo.launch()
 
1
+ import os
2
+ os.environ["OMP_NUM_THREADS"] = "1"
3
+
4
  import gradio as gr
5
  import torch
6
  import re
 
59
  )
60
  print("Model loaded successfully!")
61
 
62
+
63
+ def format_output(xml_response):
64
+ def extract_field(field_name):
65
+ pattern = f'<{field_name}>(.*?)</{field_name}>'
66
+ match = re.search(pattern, xml_response, re.DOTALL)
67
+ return match.group(1).strip() if match else 'ERROR'
68
+
69
+ argument = extract_field('argument')
70
+ claim = extract_field('claim')
71
+ explanation = extract_field('explanation')
72
+ verification = extract_field('human_verification_needed')
73
+
74
+ if argument != 'NA' and argument != 'ERROR':
75
+ return f"""✅ **Argument Found**
76
+
77
+ **Argument:** {argument}
78
+
79
+ **Claim:** {claim}
80
+
81
+ **Explanation:** {explanation}
82
+
83
+ **Verification Needed:** {verification}"""
84
+ else:
85
+ return """❌ **No Argument Found**
86
+
87
+ The text does not contain an argumentative unit."""
88
+
89
+
90
  @spaces.GPU
91
  def extract_arguments(text, temperature=0.1):
92
  if not text or not text.strip():
 
113
  temperature = 0.3
114
  except:
115
  temperature = 0.1
116
+
117
  with torch.no_grad():
118
  outputs = model.generate(
119
  **inputs,
 
128
  generated_tokens = outputs[0][input_length:]
129
  response = tokenizer.decode(generated_tokens, skip_special_tokens=True)
130
 
 
 
131
  # Fix XML start
132
  if not response.startswith('<argument>'):
133
  arg_start = response.find('<argument>')
 
137
  formatted = format_output(response)
138
  return response, formatted
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
  # Gradio interface
142
  demo = gr.Interface(
 
158
  ]
159
  )
160
 
 
161
  if __name__ == "__main__":
162
  demo.launch()