Update app.py again
Browse files
app.py
CHANGED
|
@@ -1,25 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
chat = pipeline("text-generation", model="redmint/studybuddy-ai", device
|
| 6 |
|
|
|
|
| 7 |
def respond(message, history):
|
| 8 |
-
#
|
| 9 |
context = ""
|
| 10 |
-
for
|
| 11 |
-
context += f"
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="
|
| 23 |
msg.submit(respond, [msg, chatbot], [chatbot, chatbot])
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
| 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()
|