Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,83 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
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 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
|
|
|
|
|
62 |
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from datasets import load_dataset
|
3 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
4 |
+
import random
|
5 |
+
import groq # Assuming you are using the Groq library
|
6 |
+
|
7 |
+
# Load the dataset
|
8 |
+
ds = load_dataset("Amod/mental_health_counseling_conversations")
|
9 |
+
|
10 |
+
# Extract columns (updated to match dataset column names)
|
11 |
+
context = ds["train"]["Context"] # Column name is 'Context'
|
12 |
+
response = ds["train"]["Response"] # Column name is 'Response'
|
13 |
+
|
14 |
+
# Load T5 model (small version)
|
15 |
+
model_name = "t5-small"
|
16 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
17 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
18 |
+
|
19 |
+
# Directly input the Groq API key (replace with your actual API key)
|
20 |
+
api_key = "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"
|
21 |
+
client = groq.Client(api_key=api_key)
|
22 |
+
|
23 |
+
# Function to simulate conversation
|
24 |
+
def chatbot(user_input):
|
25 |
+
if not user_input.strip():
|
26 |
+
return "Please enter a question or concern to receive guidance."
|
27 |
+
|
28 |
+
# Calculate the word count and remaining characters for the input
|
29 |
+
word_count = len(user_input.split())
|
30 |
+
max_words = 50 # Max words allowed for input
|
31 |
+
remaining_words = max_words - word_count
|
32 |
+
|
33 |
+
if remaining_words < 0:
|
34 |
+
return f"Your input is too long. Please limit to {max_words} words. Words remaining: 0."
|
35 |
+
|
36 |
+
# Try using the Groq API for the personalized response
|
37 |
+
try:
|
38 |
+
brief_response = client.predict(user_input) # Make sure this method exists for your Groq client
|
39 |
+
except Exception as e:
|
40 |
+
brief_response = None # If Groq fails, fall back to dataset
|
41 |
+
|
42 |
+
if brief_response:
|
43 |
+
return f"**Personalized Response:** {brief_response}"
|
44 |
+
|
45 |
+
# If Groq API does not work, fallback to dataset
|
46 |
+
idx = random.randint(0, len(context) - 1)
|
47 |
+
context_text = context[idx]
|
48 |
+
response_text = response[idx]
|
49 |
+
|
50 |
+
# Generate response using T5 (RAG approach)
|
51 |
+
inputs = tokenizer.encode("summarize: " + user_input, return_tensors="pt", max_length=512, truncation=True)
|
52 |
+
summary_ids = model.generate(inputs, max_length=100, num_beams=4, early_stopping=True)
|
53 |
+
generated_response = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
54 |
+
|
55 |
+
if not generated_response:
|
56 |
+
return "Oops, sorry, I don't have information about your specific problem. Please visit a doctor to prevent mishaps."
|
57 |
+
|
58 |
+
# Final response combining generated answer and dataset info
|
59 |
+
complete_response = (
|
60 |
+
f"**Contextual Information:**\n{context_text}\n\n"
|
61 |
+
f"**Generated Response:**\n{generated_response}\n\n"
|
62 |
+
f"**Fallback Response:**\n{response_text}"
|
63 |
+
)
|
64 |
+
|
65 |
+
return f"{complete_response}\n\nWords entered: {word_count}, Words remaining: {remaining_words}"
|
66 |
+
|
67 |
+
# Gradio interface setup
|
68 |
+
interface = gr.Interface(
|
69 |
+
fn=chatbot,
|
70 |
+
inputs=gr.Textbox(
|
71 |
+
label="Ask your question:",
|
72 |
+
placeholder="Describe how you're feeling today...",
|
73 |
+
lines=4
|
74 |
+
),
|
75 |
+
outputs=gr.Markdown(label="Psychologist Assistant Response"),
|
76 |
+
title="Virtual Psychiatrist Assistant",
|
77 |
+
description="Enter your mental health concerns, and receive guidance and responses from a trained assistant.",
|
78 |
+
theme="huggingface", # Optional: apply a theme if available
|
79 |
)
|
80 |
|
81 |
+
# Launch the app
|
82 |
+
interface.launch()
|
83 |
|
|
|
|