Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,53 @@
|
|
1 |
-
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
import os
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
def home():
|
13 |
-
# Render the HTML template
|
14 |
-
return render_template("index.html")
|
15 |
-
|
16 |
-
@app.route("/message", methods=["POST"])
|
17 |
-
def fetch_message():
|
18 |
-
data = request.json
|
19 |
-
message = data.get("text", "")
|
20 |
-
if not message:
|
21 |
-
return jsonify({"error": "No input provided."}), 400
|
22 |
-
|
23 |
-
# Define model parameters
|
24 |
-
model_params = {
|
25 |
-
"temperature": 0.7, # Controls randomness
|
26 |
-
"top_p": 0.9, # Nucleus sampling
|
27 |
-
"max_length": 300, # Limit response length
|
28 |
-
"do_sample": True # Enable sampling
|
29 |
-
}
|
30 |
-
|
31 |
-
# Process the message using the Hugging Face model
|
32 |
try:
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
except Exception as e:
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
|
|
41 |
if __name__ == "__main__":
|
42 |
-
|
43 |
-
port = int(os.getenv("PORT", 7860))
|
44 |
-
app.run(host="0.0.0.0", port=port)
|
45 |
|
|
|
1 |
+
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
+
# HuggingFace API Client
|
5 |
+
client = InferenceClient(model="Futuresony/future_ai_12_10_2024.gguf")
|
6 |
|
7 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
8 |
+
"""
|
9 |
+
Function to process user messages and return AI responses.
|
10 |
+
"""
|
11 |
+
messages = [{"role": "system", "content": system_message}]
|
12 |
+
|
13 |
+
# Append conversation history
|
14 |
+
for user, assistant in history:
|
15 |
+
if user:
|
16 |
+
messages.append({"role": "user", "content": user})
|
17 |
+
if assistant:
|
18 |
+
messages.append({"role": "assistant", "content": assistant})
|
19 |
+
|
20 |
+
# Add user message
|
21 |
+
messages.append({"role": "user", "content": message})
|
22 |
|
23 |
+
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
try:
|
25 |
+
# Stream AI-generated responses
|
26 |
+
for chunk in client.chat_completion(
|
27 |
+
messages=messages,
|
28 |
+
max_tokens=max_tokens,
|
29 |
+
stream=True,
|
30 |
+
temperature=temperature,
|
31 |
+
top_p=top_p,
|
32 |
+
):
|
33 |
+
token = chunk.choices[0].delta.content
|
34 |
+
response += token
|
35 |
+
yield response
|
36 |
except Exception as e:
|
37 |
+
yield f"Error: {str(e)}"
|
38 |
+
|
39 |
+
# Gradio Chat Interface
|
40 |
+
demo = gr.ChatInterface(
|
41 |
+
fn=respond,
|
42 |
+
inputs=[
|
43 |
+
gr.Textbox(value="You are a helpful assistant.", label="System Prompt"),
|
44 |
+
gr.Slider(1, 2048, value=512, step=1, label="Max Tokens"),
|
45 |
+
gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
|
46 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-P"),
|
47 |
+
],
|
48 |
+
)
|
49 |
|
50 |
+
# Run Gradio App
|
51 |
if __name__ == "__main__":
|
52 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
53 |
|