Jyotiyadav commited on
Commit
0d1ca06
·
verified ·
1 Parent(s): eb45f55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -2
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import torch
 
3
  import os
4
  auth_token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
5
  from unsloth import FastLanguageModel
@@ -29,6 +30,26 @@ From the given email, extract the following key values. The keys are explained b
29
  """
30
 
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # Define the function for generating output based on input
33
  def generate_output(input_text,model):
34
  # Prompt for the instruction
@@ -59,7 +80,8 @@ def generate_output(input_text,model):
59
  # Generate outputs
60
  outputs = model.generate(**inputs, max_new_tokens=2048, use_cache=True)
61
  output = tokenizer.batch_decode(outputs)
62
- return output
 
63
 
64
 
65
  model_options = ["sxandie/llama_3_8b_4bitQ","DataIntelligenceTeam/NER-Phi-3-mini-4k-instruct"]
@@ -68,10 +90,15 @@ inputs = [
68
  gr.inputs.Dropdown(label="Model", choices=model_options, default=model_options[0])
69
  ]
70
 
 
 
 
 
 
71
  # Create Gradio interface
72
  iface = gr.Interface(fn=generate_output,
73
  inputs=inputs,
74
- outputs="text",
75
  title="Email Information Extraction",
76
  description="Extract key information from the provided email.")
77
  iface.launch()
 
1
  import gradio as gr
2
  import torch
3
+ import re
4
  import os
5
  auth_token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
6
  from unsloth import FastLanguageModel
 
30
  """
31
 
32
 
33
+
34
+ def process_output(output):
35
+ """
36
+ Process the output to extract the response.
37
+ """
38
+ # Define the regex pattern
39
+ pattern = r'### Response:\n?(.*?)<\|endoftext\|>'
40
+ # Search for the pattern in the output
41
+ match = re.search(pattern, output, re.DOTALL)
42
+
43
+ if match:
44
+ # Extract the response
45
+ response = match.group(1)
46
+ # Remove specified symbols
47
+ cleaned_str = re.sub(r'\\n|\\\\|\\\'', '', response)
48
+ return cleaned_str
49
+ else:
50
+ return output
51
+
52
+
53
  # Define the function for generating output based on input
54
  def generate_output(input_text,model):
55
  # Prompt for the instruction
 
80
  # Generate outputs
81
  outputs = model.generate(**inputs, max_new_tokens=2048, use_cache=True)
82
  output = tokenizer.batch_decode(outputs)
83
+ cleaned_response = process_output(output)
84
+ return output,cleaned_response
85
 
86
 
87
  model_options = ["sxandie/llama_3_8b_4bitQ","DataIntelligenceTeam/NER-Phi-3-mini-4k-instruct"]
 
90
  gr.inputs.Dropdown(label="Model", choices=model_options, default=model_options[0])
91
  ]
92
 
93
+ outputs = [
94
+ gr.outputs.Textbox(label="Original Output Text"),
95
+ gr.outputs.Textbox(label="Formatted JSON")
96
+ ]
97
+
98
  # Create Gradio interface
99
  iface = gr.Interface(fn=generate_output,
100
  inputs=inputs,
101
+ outputs= outputs,
102
  title="Email Information Extraction",
103
  description="Extract key information from the provided email.")
104
  iface.launch()