Spaces:
Sleeping
Sleeping
Alioune Lesse DIOP commited on
Commit ·
291d462
1
Parent(s): 473963a
ai generator logic
Browse files- .gitignore +1 -0
- .vscode/launch.json +18 -0
- Dockerfile +1 -1
- README.md +1 -1
- main.py +5 -1
- main_v2.py +147 -0
- requirements.txt +2 -1
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
**/__pycache__/
|
.vscode/launch.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
// Use IntelliSense to learn about possible attributes.
|
| 3 |
+
// Hover to view descriptions of existing attributes.
|
| 4 |
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
| 5 |
+
"version": "0.2.0",
|
| 6 |
+
"configurations": [
|
| 7 |
+
|
| 8 |
+
{
|
| 9 |
+
"name": "Python: Module",
|
| 10 |
+
"type": "python",
|
| 11 |
+
"request": "launch",
|
| 12 |
+
"module": "uvicorn",
|
| 13 |
+
// "args": ["main:app","--reload"],
|
| 14 |
+
"args": ["main_v2:app","--reload"],
|
| 15 |
+
"console": "integratedTerminal"
|
| 16 |
+
}
|
| 17 |
+
]
|
| 18 |
+
}
|
Dockerfile
CHANGED
|
@@ -8,4 +8,4 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
|
| 11 |
-
CMD ["uvicorn", "
|
|
|
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
|
| 11 |
+
CMD ["uvicorn", "main_v2:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: 📚
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: red
|
|
|
|
| 1 |
---
|
| 2 |
+
title: MIXTRAL-FASTAPI
|
| 3 |
emoji: 📚
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: red
|
main.py
CHANGED
|
@@ -5,9 +5,9 @@ import uvicorn
|
|
| 5 |
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
-
|
| 9 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 10 |
|
|
|
|
| 11 |
class Item(BaseModel):
|
| 12 |
prompt: str
|
| 13 |
history: list
|
|
@@ -46,9 +46,13 @@ def generate(item: Item):
|
|
| 46 |
|
| 47 |
for response in stream:
|
| 48 |
output += response.token.text
|
|
|
|
| 49 |
return output
|
| 50 |
|
| 51 |
@app.post("/generate/")
|
| 52 |
async def generate_text(item: Item):
|
| 53 |
return {"response": generate(item)}
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
|
|
| 8 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 9 |
|
| 10 |
+
|
| 11 |
class Item(BaseModel):
|
| 12 |
prompt: str
|
| 13 |
history: list
|
|
|
|
| 46 |
|
| 47 |
for response in stream:
|
| 48 |
output += response.token.text
|
| 49 |
+
# print(response.token.text)
|
| 50 |
return output
|
| 51 |
|
| 52 |
@app.post("/generate/")
|
| 53 |
async def generate_text(item: Item):
|
| 54 |
return {"response": generate(item)}
|
| 55 |
|
| 56 |
+
@app.get("/test-api/")
|
| 57 |
+
async def testAPI():
|
| 58 |
+
return "API is running"
|
main_v2.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
+
import uvicorn
|
| 6 |
+
from paho.mqtt import client as mqtt_client
|
| 7 |
+
import uuid
|
| 8 |
+
import time
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
origins = [
|
| 14 |
+
"*",
|
| 15 |
+
# "http://localhost:8000",
|
| 16 |
+
]
|
| 17 |
+
app.add_middleware(
|
| 18 |
+
CORSMiddleware,
|
| 19 |
+
allow_origins=origins,
|
| 20 |
+
allow_credentials=True,
|
| 21 |
+
allow_methods=["*"],
|
| 22 |
+
allow_headers=["*"],
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
inferenceClient = InferenceClient(
|
| 26 |
+
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 27 |
+
token="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"
|
| 28 |
+
)
|
| 29 |
+
historyMap = {}
|
| 30 |
+
|
| 31 |
+
broker_host = "broker.emqx.io"
|
| 32 |
+
broker_port = 1883
|
| 33 |
+
broker_topic_prefix = "/diopsoft/mixtral-fastapi"
|
| 34 |
+
# mqttClient = None
|
| 35 |
+
mqttClient = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1, str(uuid.uuid1()))
|
| 36 |
+
def connectMqttBroker():
|
| 37 |
+
try:
|
| 38 |
+
res = mqttClient.connect(broker_host, broker_port, 60)
|
| 39 |
+
print('MQTT connection status : ' + str(res))
|
| 40 |
+
mqttClient.loop_start()
|
| 41 |
+
except Exception as Argument:
|
| 42 |
+
# logging.getLogger().exception("Failed to connect to MQTT Broker")
|
| 43 |
+
# logging.getLogger().error(e)
|
| 44 |
+
traceback.print_exc()
|
| 45 |
+
def on_connect(client, userdata, flags, rc):
|
| 46 |
+
if rc == 0:
|
| 47 |
+
print("Connected to MQTT Broker!")
|
| 48 |
+
else:
|
| 49 |
+
raise Exception("Failed to connect to MQTT Broker, return code " + rc)
|
| 50 |
+
mqttClient.on_connect = on_connect
|
| 51 |
+
# connectMqttBroker()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class Item(BaseModel):
|
| 55 |
+
is_mock: bool = False
|
| 56 |
+
token: str = None
|
| 57 |
+
mqttTopic: str = None
|
| 58 |
+
prompt: str
|
| 59 |
+
history: list = None
|
| 60 |
+
# system_prompt: str = "Soyez un assistant utile, en français de préférence"
|
| 61 |
+
system_prompt: str = "Soyez un assistant utile"
|
| 62 |
+
temperature: float = 0.7
|
| 63 |
+
max_new_tokens: int = 1048
|
| 64 |
+
top_p: float = 0.9
|
| 65 |
+
repetition_penalty: float = 1.1
|
| 66 |
+
|
| 67 |
+
def format_prompt(message, history):
|
| 68 |
+
prompt = "<s>"
|
| 69 |
+
if history:
|
| 70 |
+
for hist in history:
|
| 71 |
+
prompt += f"[INST] {hist['user_prompt']} [/INST]"
|
| 72 |
+
prompt += f" {hist['bot_response']}</s> "
|
| 73 |
+
prompt += f"[INST] {message} [/INST]"
|
| 74 |
+
return prompt
|
| 75 |
+
|
| 76 |
+
def generate(item: Item):
|
| 77 |
+
temperature = float(item.temperature)
|
| 78 |
+
if temperature < 1e-2:
|
| 79 |
+
temperature = 1e-2
|
| 80 |
+
top_p = float(item.top_p)
|
| 81 |
+
|
| 82 |
+
generate_kwargs = dict(
|
| 83 |
+
temperature=temperature,
|
| 84 |
+
max_new_tokens=item.max_new_tokens,
|
| 85 |
+
top_p=top_p,
|
| 86 |
+
repetition_penalty=item.repetition_penalty,
|
| 87 |
+
do_sample=True,
|
| 88 |
+
seed=42,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
if item.mqttTopic and not mqttClient.is_connected():
|
| 92 |
+
connectMqttBroker()
|
| 93 |
+
|
| 94 |
+
token = item.token
|
| 95 |
+
history = None
|
| 96 |
+
if token:
|
| 97 |
+
history = historyMap.get(token)
|
| 98 |
+
|
| 99 |
+
formatted_prompt = format_prompt(f"{item.system_prompt}, {item.prompt}", history)
|
| 100 |
+
if not item.is_mock:
|
| 101 |
+
stream = inferenceClient.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 102 |
+
output = ""
|
| 103 |
+
|
| 104 |
+
if not item.is_mock:
|
| 105 |
+
for response in stream:
|
| 106 |
+
print(response.token.text)
|
| 107 |
+
output += response.token.text
|
| 108 |
+
if item.mqttTopic and mqttClient.is_connected():
|
| 109 |
+
# result = mqttClient.publish(broker_topic_prefix+'/'+item.mqttTopic, response.token.text)
|
| 110 |
+
result = mqttClient.publish(broker_topic_prefix+'/'+item.mqttTopic, output)
|
| 111 |
+
status = result[0]
|
| 112 |
+
if status == 0:
|
| 113 |
+
print("Message sent to topic")
|
| 114 |
+
else:
|
| 115 |
+
print("Failed to send message to topic")
|
| 116 |
+
else:
|
| 117 |
+
for i in range(15):
|
| 118 |
+
text = " generated text " + str(i)
|
| 119 |
+
time.sleep(0.5)
|
| 120 |
+
print(text)
|
| 121 |
+
output += text
|
| 122 |
+
if item.mqttTopic and mqttClient.is_connected():
|
| 123 |
+
# result = mqttClient.publish(broker_topic_prefix+'/'+item.mqttTopic, text)
|
| 124 |
+
result = mqttClient.publish(broker_topic_prefix+'/'+item.mqttTopic, output)
|
| 125 |
+
status = result[0]
|
| 126 |
+
if status == 0:
|
| 127 |
+
print("Message sent to topic")
|
| 128 |
+
else:
|
| 129 |
+
print("Failed to send message to topic")
|
| 130 |
+
|
| 131 |
+
if item.mqttTopic and mqttClient.is_connected():
|
| 132 |
+
mqttClient.unsubscribe(broker_topic_prefix+'/'+item.mqttTopic)
|
| 133 |
+
|
| 134 |
+
if token:
|
| 135 |
+
if not historyMap.get(token):
|
| 136 |
+
historyMap[token] = []
|
| 137 |
+
historyMap.get(token).append({ "user_prompt":item.prompt, "bot_response":output })
|
| 138 |
+
|
| 139 |
+
return output
|
| 140 |
+
|
| 141 |
+
@app.post("/generate/")
|
| 142 |
+
async def generate_text(item: Item):
|
| 143 |
+
return {"response": generate(item)}
|
| 144 |
+
|
| 145 |
+
@app.get("/test-api/")
|
| 146 |
+
async def testAPI():
|
| 147 |
+
return "API V2 is running"
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
huggingface_hub
|
| 4 |
-
pydantic
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
huggingface_hub
|
| 4 |
+
pydantic
|
| 5 |
+
paho-mqtt
|