peace4ever commited on
Commit
80b63f3
·
verified ·
1 Parent(s): 19ade16

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +14 -0
  2. app.py +86 -0
  3. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ WORKDIR /code
7
+
8
+ COPY ./requirements.txt /code/requirements.txt
9
+
10
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
11
+
12
+ COPY . .
13
+
14
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "peace4ever-peace4ever/software-project-Sentiment_Analysis.hf.space"]
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import streamlit as st
2
+
3
+ # from transformers import pipeline
4
+
5
+ # model_name = "peace4ever/roberta-large-finetuned-mongolian_v4"
6
+ # pipeline = pipeline(task="sentiment-analysis", model=model_name) # Load pre-trained pipeline
7
+
8
+ # st.title("Эерэг? Сөрөг эсвэл аль нь ч биш?")
9
+ # text = st.text_area("Өгүүлбэр оруулна уу?")
10
+
11
+ # if text is not None:
12
+ # predictions = pipeline(text)
13
+ # label = predictions[0]["label"]
14
+ # probability = predictions[0]["score"]
15
+ # col1, col2 = st.columns(2)
16
+ # col1.header("Sentiment")
17
+ # col2.header("Probability")
18
+
19
+ # if label == "entailment":
20
+ # sentiment = "Negative"
21
+ # elif label == "contradiction":
22
+ # sentiment = "Neutral"
23
+ # elif label == "neutral":
24
+ # sentiment = "Positive"
25
+
26
+ # col1.write(sentiment)
27
+ # col2.write(f"{probability:.2f}")
28
+
29
+ from transformers import pipeline
30
+ from tkinter import *
31
+
32
+ # Load pre-trained sentiment analysis pipeline (replace with your actual model name)
33
+ model_name = "peace4ever/roberta-large-finetuned-mongolian_v4"
34
+ pipeline = pipeline(task="sentiment-analysis", model=model_name)
35
+
36
+ def analyze_sentiment(text):
37
+ """
38
+ This function takes user input, performs sentiment analysis using your fine-tuned model,
39
+ maps the predicted labels to desired sentiment categories, and returns the sentiment.
40
+ """
41
+ predictions = pipeline(text)
42
+ label = predictions[0]["label"]
43
+ probability = predictions[0]["score"]
44
+
45
+ sentiment_map = {
46
+ "entailment": "Negative", # Map based on your fine-tuned model's labels
47
+ "contradiction": "Neutral",
48
+ "neutral": "Positive",
49
+ # Add more mappings if needed
50
+ }
51
+
52
+ sentiment = sentiment_map.get(label.upper(), "Unknown")
53
+ return sentiment
54
+
55
+ def main():
56
+ """
57
+ This function creates the main window and handles user interaction.
58
+ """
59
+ window = Tk()
60
+ window.title("Эерэг? Сөрөг эсвэл аль нь ч биш?")
61
+
62
+ # Text box for user input
63
+ text_box = Text(window)
64
+ text_box.pack(padx=10, pady=10)
65
+
66
+ # Button to trigger sentiment analysis
67
+ analyze_button = Button(window, text="Анализ хийх", command=lambda: update_sentiment(text_box.get("1.0", END)))
68
+ analyze_button.pack(pady=10)
69
+
70
+ # Label to display sentiment result
71
+ sentiment_label = Label(window, text="")
72
+ sentiment_label.pack()
73
+
74
+ def update_sentiment(text):
75
+ """
76
+ This function performs sentiment analysis and updates the sentiment label.
77
+ """
78
+ sentiment = analyze_sentiment(text)
79
+ sentiment_label.config(text=f"Sentiment: {sentiment}")
80
+
81
+ window.mainloop()
82
+
83
+ if __name__ == "__main__":
84
+ main()
85
+
86
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ transformers
3
+ tkinter