Abu1998 commited on
Commit
7b568b1
·
verified ·
1 Parent(s): 911f9a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -84
app.py CHANGED
@@ -1,87 +1,42 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- # Initialize the inference client with the model
5
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
-
7
- def classify_text(
8
- text,
9
- max_tokens,
10
- temperature,
11
- top_p
12
- ):
13
- # Define system message with instructions for the model
14
- system_message = (
15
- "You are tasked with labeling text data based on both emotion temperature and text type categories. "
16
- "The final output must be a 13-character code that consists of the following structure:\n\n"
17
- " 0 index : Emotion Temperature code\n"
18
- " 1 index : Informative\n"
19
- " 2 index : Hate\n"
20
- " 3 index : Toxic\n"
21
- " 4 index : Appreciation\n"
22
- " 5 index : Constructive Criticism\n"
23
- " 6 index : Genuine Questions\n"
24
- " 7 index : Advice/Suggestions\n"
25
- " 8 index : Requests\n"
26
- " 9 index : Spam\n"
27
- " 10 index : Off-Topic\n"
28
- " 11 index : Engagement Boosters\n"
29
- " 12 index : Neutral/General\n\n"
30
- "Every index should have a number between 0-9. 0 means not applicable, 4 means normal, 9 means high. "
31
- "Choose appropriate numbers to showcase how much each category is related to the text input.\n\n"
32
- "Output Format:\n"
33
- "Always return a 13-character code following this structure."
34
- )
35
-
36
- # Prepare the messages, starting with the system message
37
- messages = [{"role": "system", "content": system_message}]
38
-
39
- # Add the user message
40
- messages.append({"role": "user", "content": text})
41
 
42
- response = ""
43
-
44
- # Stream the response from the model
45
- for msg in client.chat_completion(
46
- messages,
47
- max_tokens=max_tokens,
48
- stream=True,
49
- temperature=temperature,
50
- top_p=top_p,
51
- ):
52
- token = msg.choices[0].delta.content
53
- response += token
54
-
55
- # Validate the response format
56
- if len(response) == 13 and all(c.isdigit() for c in response):
57
- return response
58
- else:
59
- return "Invalid response format"
60
-
61
- # Create the Gradio interface
62
- demo = gr.Interface(
63
- fn=classify_text,
64
- inputs=[
65
- gr.Textbox(
66
- lines=5,
67
- label="Input Comment",
68
- placeholder="Enter a comment to classify",
69
- ),
70
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
71
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
72
- gr.Slider(
73
- minimum=0.1,
74
- maximum=1.0,
75
- value=0.95,
76
- step=0.05,
77
- label="Top-p (nucleus sampling)",
78
- ),
79
- ],
80
- outputs=gr.Textbox(
81
- label="13-Character Code",
82
- placeholder="The generated 13-character code will appear here",
83
- ),
84
  )
85
 
86
- if __name__ == "__main__":
87
- demo.launch()
 
1
+ # app.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ import gradio as gr
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+ import torch
6
+ import numpy as np
7
+
8
+ # Load the model and tokenizer
9
+ model_name = "huggingface/zephyr-7b-beta" # Replace this with the actual model path
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ model = AutoModelForCausalLM.from_pretrained(model_name)
12
+
13
+ def get_category_scores(text):
14
+ # Tokenize input text
15
+ inputs = tokenizer(text, return_tensors="pt")
16
+
17
+ # Generate model outputs
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+
21
+ # Process model output to extract labels
22
+ # For demonstration, we'll mock the extraction with random values
23
+ # Replace this part with actual logic based on model output
24
+
25
+ # Example random values for demonstration (these should be extracted from model)
26
+ scores = np.random.randint(0, 10, size=13) # Replace with actual logic
27
+
28
+ # Convert scores to 13-digit code
29
+ result_code = ''.join(map(str, scores))
30
+ return result_code
31
+
32
+ # Define the Gradio interface
33
+ iface = gr.Interface(
34
+ fn=get_category_scores,
35
+ inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
36
+ outputs="text",
37
+ title="Text Labeling",
38
+ description="Label text data based on emotion temperature and categories. Returns a 13-digit code."
 
 
 
 
 
 
39
  )
40
 
41
+ # Launch the interface
42
+ iface.launch()