redmint commited on
Commit
2b4a1c2
·
verified ·
1 Parent(s): 3e2523a

Update app.py again

Browse files
Files changed (1) hide show
  1. app.py +29 -12
app.py CHANGED
@@ -1,25 +1,42 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load the model (use GPU if available)
5
- chat = pipeline("text-generation", model="redmint/studybuddy-ai", device=0)
6
 
 
7
  def respond(message, history):
8
- # Combine history with new message for context
9
  context = ""
10
- for h in history:
11
- context += f"You: {h[0]}\nAI: {h[1]}\n"
12
- input_text = context + f"You: {message}\nAI:"
13
-
14
- output = chat(input_text, max_new_tokens=300)[0]['generated_text']
15
- # Extract AI response
16
- response = output.split("AI:")[-1].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  history.append((message, response))
18
  return history, history
19
 
 
20
  with gr.Blocks() as demo:
21
  chatbot = gr.Chatbot()
22
- msg = gr.Textbox(label="Your question")
23
  msg.submit(respond, [msg, chatbot], [chatbot, chatbot])
24
 
25
- demo.launch()
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load your StudyBuddyAI model
5
+ chat = pipeline("text-generation", model="redmint/studybuddy-ai", device=-1) # use -1 for CPU, 0 for GPU
6
 
7
+ # Core response function
8
  def respond(message, history):
9
+ # Convert history into simple Q&A format
10
  context = ""
11
+ for user, bot in history:
12
+ context += f"Question: {user}\nAnswer: {bot}\n"
13
+
14
+ # Add the latest user question
15
+ prompt = context + f"Question: {message}\nAnswer:"
16
+
17
+ # Generate response
18
+ output = chat(
19
+ prompt,
20
+ max_new_tokens=200,
21
+ temperature=0.4,
22
+ top_p=0.9,
23
+ do_sample=True,
24
+ )[0]["generated_text"]
25
+
26
+ # Extract only the part after "Answer:"
27
+ if "Answer:" in output:
28
+ response = output.split("Answer:")[-1].strip()
29
+ else:
30
+ response = output.strip()
31
+
32
  history.append((message, response))
33
  return history, history
34
 
35
+ # Gradio interface
36
  with gr.Blocks() as demo:
37
  chatbot = gr.Chatbot()
38
+ msg = gr.Textbox(label="Ask StudyBuddyAI")
39
  msg.submit(respond, [msg, chatbot], [chatbot, chatbot])
40
 
41
+ if __name__ == "__main__":
42
+ demo.launch()