Update app.py
Browse files
app.py
CHANGED
@@ -38,20 +38,21 @@ except OSError as e:
|
|
38 |
|
39 |
|
40 |
# --- Chat Function ---
|
41 |
-
|
42 |
def chat_with_llm(prompt, history):
|
43 |
-
"""Generates a response from the LLM."""
|
44 |
-
|
45 |
formatted_prompt = ""
|
46 |
if history:
|
47 |
-
for
|
48 |
-
|
49 |
-
|
|
|
|
|
50 |
|
51 |
formatted_prompt += f"{tokenizer.bos_token}{prompt}{tokenizer.eos_token}"
|
|
|
52 |
try:
|
53 |
-
|
54 |
-
|
55 |
formatted_prompt,
|
56 |
max_new_tokens=256,
|
57 |
do_sample=True,
|
@@ -61,19 +62,20 @@ def chat_with_llm(prompt, history):
|
|
61 |
return_full_text=False,
|
62 |
pad_token_id=tokenizer.eos_token_id,
|
63 |
)
|
64 |
-
|
65 |
-
|
66 |
except Exception as e:
|
67 |
return f"Error during generation: {e}"
|
68 |
|
|
|
|
|
69 |
# --- Gradio Interface ---
|
70 |
# Use the 'messages' format for chatbot
|
71 |
def predict(message, history):
|
72 |
history = history or []
|
73 |
response = chat_with_llm(message, history)
|
74 |
-
|
75 |
-
history.append({"role": "
|
76 |
-
history.append({"role": "assistant", "content": response}) # Append assistant (AI) message
|
77 |
return "", history
|
78 |
|
79 |
with gr.Blocks() as demo:
|
@@ -83,9 +85,7 @@ with gr.Blocks() as demo:
|
|
83 |
msg = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
84 |
clear = gr.Button("Clear")
|
85 |
|
86 |
-
|
87 |
msg.submit(predict, [msg, chatbot], [msg, chatbot])
|
88 |
-
|
89 |
-
clear.click(lambda: [], [], chatbot, queue=False) # Return empty list for history
|
90 |
|
91 |
demo.launch(share=True)
|
|
|
38 |
|
39 |
|
40 |
# --- Chat Function ---
|
|
|
41 |
def chat_with_llm(prompt, history):
|
42 |
+
"""Generates a response from the LLM, handling history correctly."""
|
|
|
43 |
formatted_prompt = ""
|
44 |
if history:
|
45 |
+
for item in history:
|
46 |
+
if item["role"] == "user":
|
47 |
+
formatted_prompt += f"{tokenizer.bos_token}{item['content']}{tokenizer.eos_token}"
|
48 |
+
elif item["role"] == "assistant":
|
49 |
+
formatted_prompt += f"{item['content']}{tokenizer.eos_token}"
|
50 |
|
51 |
formatted_prompt += f"{tokenizer.bos_token}{prompt}{tokenizer.eos_token}"
|
52 |
+
|
53 |
try:
|
54 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device_map="auto")
|
55 |
+
result = pipe(
|
56 |
formatted_prompt,
|
57 |
max_new_tokens=256,
|
58 |
do_sample=True,
|
|
|
62 |
return_full_text=False,
|
63 |
pad_token_id=tokenizer.eos_token_id,
|
64 |
)
|
65 |
+
response = result[0]['generated_text'].strip()
|
66 |
+
return response
|
67 |
except Exception as e:
|
68 |
return f"Error during generation: {e}"
|
69 |
|
70 |
+
|
71 |
+
|
72 |
# --- Gradio Interface ---
|
73 |
# Use the 'messages' format for chatbot
|
74 |
def predict(message, history):
|
75 |
history = history or []
|
76 |
response = chat_with_llm(message, history)
|
77 |
+
history.append({"role": "user", "content": message})
|
78 |
+
history.append({"role": "assistant", "content": response})
|
|
|
79 |
return "", history
|
80 |
|
81 |
with gr.Blocks() as demo:
|
|
|
85 |
msg = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
86 |
clear = gr.Button("Clear")
|
87 |
|
|
|
88 |
msg.submit(predict, [msg, chatbot], [msg, chatbot])
|
89 |
+
clear.click(lambda: [], [], chatbot, queue=False)
|
|
|
90 |
|
91 |
demo.launch(share=True)
|