Spaces:
Sleeping
Sleeping
made API legal and hidden
Browse filesbro jus tread the title of the commit
app.py
CHANGED
|
@@ -1,34 +1,47 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
chat_history = []
|
| 10 |
|
| 11 |
def chat(user_message):
|
| 12 |
-
chat_history.append(
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
return reply
|
| 28 |
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
chatbot_ui = gr.Chatbot()
|
| 31 |
-
msg = gr.Textbox(
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
+
API_URL = "https://api-inference.huggingface.co/models/EleutherAI/gpt-neo-125M"
|
| 6 |
+
|
| 7 |
+
HEADERS = {
|
| 8 |
+
"Authorization": f"Bearer {os.environ['HUGGINGFACEHUB_API_TOKEN']}"
|
| 9 |
+
}
|
| 10 |
|
| 11 |
chat_history = []
|
| 12 |
|
| 13 |
def chat(user_message):
|
| 14 |
+
chat_history.append({"role": "user", "content": user_message})
|
| 15 |
+
payload = {
|
| 16 |
+
"inputs": {
|
| 17 |
+
"past_user_inputs": [m["content"] for m in chat_history if m["role"] == "user"][:-1],
|
| 18 |
+
"generated_responses": [m["content"] for m in chat_history if m["role"] == "assistant"],
|
| 19 |
+
"text": user_message
|
| 20 |
+
},
|
| 21 |
+
"options": {"use_cache": False}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 25 |
+
response.raise_for_status()
|
| 26 |
+
result = response.json()
|
| 27 |
+
|
| 28 |
+
if isinstance(result, dict) and "generated_text" in result:
|
| 29 |
+
reply = result["generated_text"]
|
| 30 |
+
else:
|
| 31 |
+
reply = result[0].get("generated_text", "")
|
| 32 |
+
|
| 33 |
+
reply = reply.strip()
|
| 34 |
+
chat_history.append({"role": "assistant", "content": reply})
|
| 35 |
return reply
|
| 36 |
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
chatbot_ui = gr.Chatbot()
|
| 39 |
+
msg = gr.Textbox(
|
| 40 |
+
placeholder="Type your message and press Enter",
|
| 41 |
+
lines=1,
|
| 42 |
+
show_label=False
|
| 43 |
+
)
|
| 44 |
+
msg.submit(fn=chat, inputs=msg, outputs=chatbot_ui)
|
| 45 |
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|