Spaces:
Running
Running
Update
Browse files
app.py
CHANGED
|
@@ -1,66 +1,30 @@
|
|
| 1 |
-
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
""
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": val[0]})
|
| 27 |
-
if val[1]:
|
| 28 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 29 |
-
|
| 30 |
-
messages.append({"role": "user", "content": message})
|
| 31 |
-
|
| 32 |
-
response = ""
|
| 33 |
-
for message in client.chat_completion(
|
| 34 |
-
messages,
|
| 35 |
-
max_tokens=max_tokens,
|
| 36 |
-
stream=True,
|
| 37 |
-
temperature=temperature,
|
| 38 |
-
top_p=top_p,
|
| 39 |
-
):
|
| 40 |
-
token = message.choices[0].delta.content
|
| 41 |
-
|
| 42 |
-
response += token
|
| 43 |
-
yield response
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
"""
|
| 47 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 48 |
-
"""
|
| 49 |
-
demo = gr.ChatInterface(
|
| 50 |
-
respond,
|
| 51 |
-
additional_inputs=[
|
| 52 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 53 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 54 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 55 |
-
gr.Slider(
|
| 56 |
-
minimum=0.1,
|
| 57 |
-
maximum=1.0,
|
| 58 |
-
value=0.95,
|
| 59 |
-
step=0.05,
|
| 60 |
-
label="Top-p (nucleus sampling)",
|
| 61 |
-
),
|
| 62 |
-
],
|
| 63 |
)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
-
demo.launch(
|
|
|
|
| 1 |
+
# app.py
|
|
|
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
API_URL = "https://router.huggingface.co/together/v1/chat/completions"
|
| 7 |
+
headers = {
|
| 8 |
+
"Authorization": f"Bearer {os.environ['huggingface_token']}",
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
def query_api(message):
|
| 12 |
+
payload = {
|
| 13 |
+
"messages": [{"role": "user", "content": message}],
|
| 14 |
+
"model": "mistralai/Mistral-7B-Instruct-v0.3"
|
| 15 |
+
}
|
| 16 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 17 |
+
try:
|
| 18 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"Error: {e}\n\nFull response:\n{response.text}"
|
| 21 |
+
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=query_api,
|
| 24 |
+
inputs=gr.Textbox(label="Ask something"),
|
| 25 |
+
outputs="text",
|
| 26 |
+
title="Chat with AI Co-Pilot (Mistral 7B Instruct)",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
if __name__ == "__main__":
|
| 30 |
+
demo.launch()
|