Zeeshan42 commited on
Commit
fb00fc0
1 Parent(s): cfa51af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -60
app.py CHANGED
@@ -1,64 +1,83 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
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