AliArshad commited on
Commit
6e40eda
1 Parent(s): 8b44f8a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """app.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/#fileId=https%3A//huggingface.co/spaces/AliArshad/Psycho/blob/main/app.ipynb
8
+ """
9
+
10
+ pip install transformers gradio
11
+
12
+ # Load model directly
13
+ from transformers import AutoTokenizer, AutoModelForCausalLM
14
+
15
+ tokenizer = AutoTokenizer.from_pretrained("Intel/neural-chat-7b-v3-1")
16
+ model = AutoModelForCausalLM.from_pretrained("Intel/neural-chat-7b-v3-1")
17
+
18
+ def generate_response(system_input, user_input):
19
+
20
+ # Format the input using the provided template
21
+ prompt = f"### System:\n{system_input}\n### User:\n{user_input}\n### Assistant:\n"
22
+
23
+ # Tokenize and encode the prompt
24
+ inputs = tokenizer.encode(prompt, return_tensors="pt", add_special_tokens=False)
25
+
26
+ # Generate a response
27
+ outputs = model.generate(inputs, max_length=1000, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
28
+
29
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
30
+
31
+ # Extract only the assistant's response
32
+ return response.split("### Assistant:\n")[-1]
33
+
34
+ '''# Example usage
35
+ system_input = "Please act as a psychology counselor assistant. I will provide you counselling notes and you need to provide me exactly two things in return, assessment and plan for the notes. Please note that the assessment should begin with keword 'Assessment:', and plan should begin with keyword 'Plan:'. Please make it logical, simple, concise, and clear."
36
+ user_input = "Can't sleep well?"
37
+ response = generate_response(system_input, user_input)
38
+ print(response)
39
+ '''
40
+
41
+ def generate_response1(user_input):
42
+ system_input = "Please act as a psychology counselor assistant. I will provide you counselling notes and you need to provide me exactly two things in return, assessment and plan for the notes. Please note that the assessment should begin with keword 'Assessment:', and plan should begin with keyword 'Plan:'. Please make it logical, simple, concise, and clear."
43
+ prompt = f"### System:\n{system_input}\n### User:\n{user_input}\n### Assistant:\n"
44
+ inputs = tokenizer.encode(prompt, return_tensors="pt", add_special_tokens=False)
45
+ outputs = model.generate(inputs, max_length=1000, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
46
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
47
+ response_parts = response.split("### Assistant:\n")[-1]
48
+
49
+ # Split the response into Assessment and Plan
50
+ assessment = response_parts.split("Plan:")[0].strip()
51
+ plan = "Plan:" + response_parts.split("Plan:")[1].strip()
52
+
53
+ return assessment, plan
54
+
55
+ import gradio as gr
56
+ # Gradio interface
57
+ iface = gr.Interface(
58
+ fn=generate_response1,
59
+ inputs=gr.Textbox(lines=2, label="Please Upload Notes here", placeholder="Enter your notes here..."),
60
+ outputs=[gr.Textbox(label="Assessment"), gr.Textbox(label="Plan")],
61
+ examples=[
62
+ ["Can't sleep well?"],
63
+ ["Feeling anxious all the time."]
64
+ ]
65
+ )
66
+
67
+ # Launch the interface
68
+ iface.launch(share=True)