Utsav2001 commited on
Commit
bfdd25e
Β·
verified Β·
1 Parent(s): f8082bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -6,6 +6,7 @@ import os
6
 
7
  import gradio as gr
8
  from huggingface_hub import CommitScheduler
 
9
 
10
  # Setup the directory for saving data
11
  JSON_DATASET_DIR = Path("feedback_dataset")
@@ -23,22 +24,43 @@ scheduler = CommitScheduler(
23
  token=os.getenv('hf_write')
24
  )
25
 
 
 
 
 
 
 
 
 
 
 
26
  # Function to save data
27
- def save_feedback(name: str, feedback: str) -> None:
28
  with scheduler.lock:
29
  with JSON_DATASET_PATH.open("a") as f:
30
  json.dump(
31
- {"name": name, "feedback": feedback, "datetime": datetime.now().isoformat()}, f
 
 
 
 
 
 
32
  )
33
  f.write("\n")
34
  scheduler.push_to_hub(commit_message="Add new feedback")
35
 
36
  # Gradio Interface
 
 
 
 
 
37
  with gr.Blocks() as demo:
38
  with gr.Row():
39
- name_input = gr.Textbox(label="Name")
40
- feedback_input = gr.Textbox(label="Feedback")
41
- submit_button = gr.Button("Submit")
42
- submit_button.click(fn=save_feedback, inputs=[name_input, feedback_input], outputs=None)
43
 
44
  demo.launch()
 
6
 
7
  import gradio as gr
8
  from huggingface_hub import CommitScheduler
9
+ from transformers import pipeline
10
 
11
  # Setup the directory for saving data
12
  JSON_DATASET_DIR = Path("feedback_dataset")
 
24
  token=os.getenv('hf_write')
25
  )
26
 
27
+ # Initialize Sentiment Analysis Pipeline
28
+ sentiment_pipeline = pipeline("sentiment-analysis")
29
+
30
+ # Function to analyze sentiment
31
+ def analyze_sentiment(user_input: str):
32
+ result = sentiment_pipeline(user_input)[0]
33
+ sentiment = result['label']
34
+ confidence = result['score']
35
+ return sentiment, confidence
36
+
37
  # Function to save data
38
+ def save_feedback(user_input: str, sentiment: str, confidence: float) -> None:
39
  with scheduler.lock:
40
  with JSON_DATASET_PATH.open("a") as f:
41
  json.dump(
42
+ {
43
+ "review": user_input,
44
+ "sentiment": sentiment,
45
+ "confidence": confidence,
46
+ "datetime": datetime.now().isoformat(),
47
+ },
48
+ f,
49
  )
50
  f.write("\n")
51
  scheduler.push_to_hub(commit_message="Add new feedback")
52
 
53
  # Gradio Interface
54
+ def handle_feedback(user_input):
55
+ sentiment, confidence = analyze_sentiment(user_input)
56
+ save_feedback(user_input, sentiment, confidence)
57
+ return f"Sentiment: {sentiment}, Confidence: {confidence:.2f}"
58
+
59
  with gr.Blocks() as demo:
60
  with gr.Row():
61
+ user_input = gr.Textbox(label="Enter Your Review", placeholder="Type your review here...")
62
+ output = gr.Textbox(label="Analysis Result", interactive=False)
63
+ submit_button = gr.Button("Analyze and Save")
64
+ submit_button.click(fn=handle_feedback, inputs=user_input, outputs=output)
65
 
66
  demo.launch()