Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import requests
|
|
|
|
| 4 |
import uvicorn
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
from fastapi import FastAPI
|
| 7 |
from pydantic import BaseModel
|
| 8 |
|
|
|
|
| 9 |
# Initialize FastAPI
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
# Hugging Face Model for Gradio & API
|
| 13 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 14 |
|
|
|
|
| 15 |
# Telegram Bot Token (Replace with your actual token)
|
| 16 |
TELEGRAM_BOT_TOKEN = "7880465873:AAFDakza41P2drekyUzv-2o8Yp-TBRU7TiU"
|
| 17 |
TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
|
|
@@ -25,10 +28,10 @@ class Message(BaseModel):
|
|
| 25 |
def respond(
|
| 26 |
message,
|
| 27 |
history: list[tuple[str, str]],
|
| 28 |
-
system_message
|
| 29 |
-
max_tokens
|
| 30 |
-
temperature
|
| 31 |
-
top_p
|
| 32 |
):
|
| 33 |
messages = [{"role": "system", "content": system_message}]
|
| 34 |
|
|
@@ -50,15 +53,28 @@ def respond(
|
|
| 50 |
top_p=top_p,
|
| 51 |
):
|
| 52 |
token = message.choices[0].delta.content
|
|
|
|
| 53 |
response += token
|
| 54 |
yield response
|
| 55 |
|
| 56 |
-
#
|
| 57 |
demo = gr.ChatInterface(
|
| 58 |
-
respond,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 63 |
|
| 64 |
@app.post("/predict")
|
|
@@ -70,7 +86,26 @@ def predict(data: Message):
|
|
| 70 |
|
| 71 |
# Send response back to Telegram user
|
| 72 |
requests.post(TELEGRAM_API_URL, json={"chat_id": data.chat_id, "text": bot_reply})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return {"response": bot_reply}
|
| 74 |
|
|
|
|
|
|
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
+
|
| 5 |
import uvicorn
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
from fastapi import FastAPI
|
| 8 |
from pydantic import BaseModel
|
| 9 |
|
| 10 |
+
|
| 11 |
# Initialize FastAPI
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
# Hugging Face Model for Gradio & API
|
| 15 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 16 |
|
| 17 |
+
|
| 18 |
# Telegram Bot Token (Replace with your actual token)
|
| 19 |
TELEGRAM_BOT_TOKEN = "7880465873:AAFDakza41P2drekyUzv-2o8Yp-TBRU7TiU"
|
| 20 |
TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
|
|
|
|
| 28 |
def respond(
|
| 29 |
message,
|
| 30 |
history: list[tuple[str, str]],
|
| 31 |
+
system_message,
|
| 32 |
+
max_tokens,
|
| 33 |
+
temperature,
|
| 34 |
+
top_p,
|
| 35 |
):
|
| 36 |
messages = [{"role": "system", "content": system_message}]
|
| 37 |
|
|
|
|
| 53 |
top_p=top_p,
|
| 54 |
):
|
| 55 |
token = message.choices[0].delta.content
|
| 56 |
+
|
| 57 |
response += token
|
| 58 |
yield response
|
| 59 |
|
| 60 |
+
# Gradio UI
|
| 61 |
demo = gr.ChatInterface(
|
| 62 |
+
respond,
|
| 63 |
+
additional_inputs=[
|
| 64 |
+
gr.Textbox(value="You are a helpful Assistant. You let the user to know if the information they share with you is true or false. You return the actual facts behind the information given, after stating the state of Truth or False.", label="System message"),
|
| 65 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 66 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 67 |
+
gr.Slider(
|
| 68 |
+
minimum=0.1,
|
| 69 |
+
maximum=1.0,
|
| 70 |
+
value=0.95,
|
| 71 |
+
step=0.05,
|
| 72 |
+
label="Top-p (nucleus sampling)",
|
| 73 |
+
),
|
| 74 |
+
],
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# Mount Gradio UI on the root `/`
|
| 78 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 79 |
|
| 80 |
@app.post("/predict")
|
|
|
|
| 86 |
|
| 87 |
# Send response back to Telegram user
|
| 88 |
requests.post(TELEGRAM_API_URL, json={"chat_id": data.chat_id, "text": bot_reply})
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
return {"response": bot_reply}
|
| 107 |
|
| 108 |
+
|
| 109 |
+
|
| 110 |
if __name__ == "__main__":
|
| 111 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|