Spaces:
Running
Running
hamaadayubkhan
commited on
Commit
•
59978cc
1
Parent(s):
8c988d8
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
inputs=gr.Textbox(label="Ask your question:", placeholder="How are you feeling today?"),
|
54 |
-
outputs=gr.Markdown(label="Psychologist Assistant Response"),
|
55 |
-
title="Virtual Psychologist Assistant",
|
56 |
-
description=(
|
57 |
-
"This is a supportive assistant designed to provide compassionate guidance "
|
58 |
-
"for mental well-being. Type your thoughts or questions for tailored advice and insights."
|
59 |
-
),
|
60 |
-
theme="huggingface", # Optional: apply a theme if available
|
61 |
)
|
62 |
|
63 |
-
# Launch
|
64 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
import os
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
# Load environment variables from .env file
|
8 |
+
load_dotenv()
|
9 |
+
HUGGING_FACE_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
|
10 |
+
|
11 |
+
# Load the sentiment analysis model
|
12 |
+
sentiment_tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
13 |
+
sentiment_model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
14 |
+
|
15 |
+
# Load the LLaMA-1B model for text generation, using the token from the environment variable
|
16 |
+
llama_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B-Instruct", use_auth_token=HUGGING_FACE_TOKEN)
|
17 |
+
llama_model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B-Instruct", use_auth_token=HUGGING_FACE_TOKEN)
|
18 |
+
|
19 |
+
# Function for sentiment analysis
|
20 |
+
def analyze_sentiment(text):
|
21 |
+
inputs = sentiment_tokenizer(text, return_tensors="pt")
|
22 |
+
outputs = sentiment_model(**inputs)
|
23 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
24 |
+
sentiment = "positive" if torch.argmax(probs) == 1 else "negative"
|
25 |
+
confidence = probs.max().item()
|
26 |
+
return sentiment, confidence
|
27 |
+
|
28 |
+
# Function to generate a supportive response
|
29 |
+
def generate_response(sentiment, text):
|
30 |
+
prompt = f"The user feels {sentiment}. Respond with supportive advice based on: {text}"
|
31 |
+
inputs = llama_tokenizer(prompt, return_tensors="pt")
|
32 |
+
response = llama_model.generate(**inputs, max_length=150)
|
33 |
+
return llama_tokenizer.decode(response[0], skip_special_tokens=True)
|
34 |
+
|
35 |
+
# Combine both functions for the personal psychologist
|
36 |
+
def personal_psychologist(text):
|
37 |
+
sentiment, confidence = analyze_sentiment(text)
|
38 |
+
response = generate_response(sentiment, text)
|
39 |
+
return f"Sentiment: {sentiment} (Confidence: {confidence:.2f})\n\nResponse: {response}"
|
40 |
+
|
41 |
+
# Set up Gradio interface
|
42 |
+
iface = gr.Interface(
|
43 |
+
fn=personal_psychologist,
|
44 |
+
inputs="text",
|
45 |
+
outputs="text",
|
46 |
+
title="Personal Psychologist",
|
47 |
+
description="A supportive AI that assesses your mood and provides comforting advice based on your input.",
|
48 |
+
examples=[
|
49 |
+
["I'm feeling very anxious and stressed about my exams."],
|
50 |
+
["I had a great day with my friends!"],
|
51 |
+
["I feel like I'm not good enough and everything is going wrong."]
|
52 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
)
|
54 |
|
55 |
+
# Launch Gradio app
|
56 |
+
iface.launch()
|