Spaces:
Paused
Paused
Updated another chat method
Browse files
app.py
CHANGED
|
@@ -4,10 +4,10 @@ from transformers import pipeline, AutoTokenizer
|
|
| 4 |
#model_id = "HuggingFaceTB/SmolLM2-135M-Instruct"
|
| 5 |
#we will be using Qwen2.5-0.5B-Instruct model is a lightweight powerhouse
|
| 6 |
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 7 |
-
pipe = pipeline("text-generation", model=model_id)
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
|
| 10 |
-
def
|
| 11 |
# 1. Format the conversation using the official template
|
| 12 |
messages = [{"role": "user", "content": message},
|
| 13 |
{"role": "system", "content": "Use context if relevant, otherwise answer generally."},]
|
|
@@ -28,4 +28,18 @@ def chat(message, history):
|
|
| 28 |
answer = generated_text[len(prompt):].split("<|im_end|>")[0].strip()
|
| 29 |
return answer
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
gr.ChatInterface(chat).launch()
|
|
|
|
| 4 |
#model_id = "HuggingFaceTB/SmolLM2-135M-Instruct"
|
| 5 |
#we will be using Qwen2.5-0.5B-Instruct model is a lightweight powerhouse
|
| 6 |
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 7 |
+
pipe = pipeline("text-generation", model=model_id, device_map="auto") #adding a auto detect gpu
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
|
| 10 |
+
def chat_old(message, history):
|
| 11 |
# 1. Format the conversation using the official template
|
| 12 |
messages = [{"role": "user", "content": message},
|
| 13 |
{"role": "system", "content": "Use context if relevant, otherwise answer generally."},]
|
|
|
|
| 28 |
answer = generated_text[len(prompt):].split("<|im_end|>")[0].strip()
|
| 29 |
return answer
|
| 30 |
|
| 31 |
+
import openai
|
| 32 |
+
def chat(message, history):
|
| 33 |
+
messages = [{"role": "user", "content": message},
|
| 34 |
+
{"role": "system", "content": "Use context if relevant, otherwise answer generally."},]
|
| 35 |
+
|
| 36 |
+
for msg in history:
|
| 37 |
+
messages.append(msg)
|
| 38 |
+
|
| 39 |
+
partial_message = ""
|
| 40 |
+
for chunk in response:
|
| 41 |
+
if chunk.choices[0].delta.content:
|
| 42 |
+
partial_message += chunk.choices[0].delta.content
|
| 43 |
+
yield partial_message
|
| 44 |
+
|
| 45 |
gr.ChatInterface(chat).launch()
|