Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,35 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
hf_client = InferenceClient("meta-llama/Meta-Llama-3.1-70B-Instruct", token=os.getenv("HF_TOKEN"))
|
| 8 |
-
|
| 9 |
-
def respond(
|
| 10 |
-
message,
|
| 11 |
-
history: list[tuple[str, str]],
|
| 12 |
-
system_message,
|
| 13 |
-
max_tokens,
|
| 14 |
-
temperature,
|
| 15 |
-
top_p,
|
| 16 |
-
):
|
| 17 |
-
system_prefix = """
|
| 18 |
-
If the input language is Korean, respond in Korean. If it's English, respond in English.
|
| 19 |
-
Do not output in both languages simultaneously. Always respond in Korean to Korean questions and in English to English questions.
|
| 20 |
-
"""
|
| 21 |
-
|
| 22 |
-
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
|
| 23 |
-
|
| 24 |
-
# Ensure alternating user/assistant messages
|
| 25 |
-
for user_msg, assistant_msg in history:
|
| 26 |
-
messages.append({"role": "user", "content": user_msg})
|
| 27 |
-
if assistant_msg: # Only add assistant message if it exists
|
| 28 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
| 29 |
-
|
| 30 |
-
# Add the current user message
|
| 31 |
-
messages.append({"role": "user", "content": message})
|
| 32 |
-
|
| 33 |
-
response = ""
|
| 34 |
|
|
|
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
theme = "Nymbo/Nymbo_Theme"
|
| 52 |
-
|
| 53 |
-
css = """
|
| 54 |
-
footer {
|
| 55 |
-
visibility: hidden;
|
| 56 |
-
}
|
| 57 |
-
"""
|
| 58 |
-
|
| 59 |
-
demo = gr.ChatInterface(
|
| 60 |
-
respond,
|
| 61 |
-
additional_inputs=[
|
| 62 |
-
gr.Textbox(value="""
|
| 63 |
-
You are an AI assistant.
|
| 64 |
-
""", label="System Prompt"),
|
| 65 |
-
gr.Slider(minimum=1, maximum=2000, 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 |
-
theme=theme, # Apply theme
|
| 76 |
-
css=css # Apply CSS
|
| 77 |
-
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from tempfile import NamedTemporaryFile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def main():
|
| 7 |
try:
|
| 8 |
+
# Get the code from secrets
|
| 9 |
+
code = os.environ.get("MAIN_CODE")
|
| 10 |
+
|
| 11 |
+
if not code:
|
| 12 |
+
st.error("⚠️ The application code wasn't found in secrets. Please add the MAIN_CODE secret.")
|
| 13 |
+
return
|
| 14 |
+
|
| 15 |
+
# Create a temporary Python file
|
| 16 |
+
with NamedTemporaryFile(suffix='.py', delete=False, mode='w') as tmp:
|
| 17 |
+
tmp.write(code)
|
| 18 |
+
tmp_path = tmp.name
|
| 19 |
+
|
| 20 |
+
# Execute the code
|
| 21 |
+
exec(compile(code, tmp_path, 'exec'), globals())
|
| 22 |
+
|
| 23 |
+
# Clean up the temporary file
|
| 24 |
+
try:
|
| 25 |
+
os.unlink(tmp_path)
|
| 26 |
+
except:
|
| 27 |
+
pass
|
| 28 |
+
|
| 29 |
except Exception as e:
|
| 30 |
+
st.error(f"⚠️ Error loading or executing the application: {str(e)}")
|
| 31 |
+
import traceback
|
| 32 |
+
st.code(traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|
| 35 |
+
main()
|