Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py on Hugging Face Space
|
2 |
+
import gradio as gr
|
3 |
+
import requests # Used for making HTTP requests to your backend
|
4 |
+
import os
|
5 |
+
|
6 |
+
# --- IMPORTANT ---
|
7 |
+
# Replace this with your actual Cloudflare Worker URL after deployment.
|
8 |
+
# You can also set this as a Hugging Face Space secret if you prefer.
|
9 |
+
BACKEND_API_URL = os.getenv("BACKEND_API_URL", "https://actor-llm-deepseek-backend.smplushypermedia.workers.dev/")
|
10 |
+
# Example: https://actor-llm-deepseek-backend.your-username.workers.dev
|
11 |
+
|
12 |
+
# Store conversation history for context
|
13 |
+
conversation_history = []
|
14 |
+
current_script_in_session = ""
|
15 |
+
current_character_in_session = ""
|
16 |
+
|
17 |
+
def get_actor_advice(user_query, script_input, character_name_input):
|
18 |
+
global conversation_history, current_script_in_session, current_character_in_session
|
19 |
+
|
20 |
+
# 1. Check if script or character changed to reset context
|
21 |
+
if script_input != current_script_in_session or character_name_input != current_character_in_session:
|
22 |
+
conversation_history = [] # Reset history
|
23 |
+
current_script_in_session = script_input
|
24 |
+
current_character_in_session = character_name_input
|
25 |
+
gr.Warning("Script or character changed! Conversation context has been reset.")
|
26 |
+
|
27 |
+
# 2. Prepare payload for the Cloudflare Worker
|
28 |
+
payload = {
|
29 |
+
"userQuery": user_query,
|
30 |
+
"scriptContent": script_input,
|
31 |
+
"characterName": character_name_input,
|
32 |
+
"conversationHistory": conversation_history # Send current history
|
33 |
+
}
|
34 |
+
|
35 |
+
headers = {"Content-Type": "application/json"}
|
36 |
+
|
37 |
+
try:
|
38 |
+
# 3. Make HTTP POST request to your Cloudflare Worker
|
39 |
+
response = requests.post(BACKEND_API_URL, json=payload, headers=headers)
|
40 |
+
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
41 |
+
|
42 |
+
response_data = response.json()
|
43 |
+
llm_response = response_data.get("response", "No advice received.")
|
44 |
+
|
45 |
+
# 4. Update conversation history with user query and LLM response
|
46 |
+
conversation_history.append({"role": "user", "content": user_query})
|
47 |
+
conversation_history.append({"role": "assistant", "content": llm_response})
|
48 |
+
|
49 |
+
return llm_response
|
50 |
+
except requests.exceptions.RequestException as e:
|
51 |
+
print(f"Error communicating with backend: {e}")
|
52 |
+
return f"Error connecting to the backend. Please ensure the backend is deployed and accessible. Details: {e}"
|
53 |
+
except Exception as e:
|
54 |
+
print(f"An unexpected error occurred: {e}")
|
55 |
+
return f"An unexpected error occurred: {e}"
|
56 |
+
|
57 |
+
# --- Frontend UI with Gradio ---
|
58 |
+
with gr.Blocks() as demo:
|
59 |
+
gr.Markdown("# Actor's LLM Assistant")
|
60 |
+
gr.Markdown("Enter your script and ask for acting advice for your character. The AI will remember past queries in the current session.")
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
with gr.Column():
|
64 |
+
script_input = gr.Textbox(
|
65 |
+
label="Paste Your Script Here",
|
66 |
+
lines=10,
|
67 |
+
placeholder="[Scene: A dimly lit stage...]\nANNA: (Whispering) 'I can't believe this...'"
|
68 |
+
)
|
69 |
+
character_name_input = gr.Textbox(
|
70 |
+
label="Your Character's Name",
|
71 |
+
placeholder="e.g., Anna"
|
72 |
+
)
|
73 |
+
# Photo customization placeholder (as discussed, for UI or future multimodal)
|
74 |
+
photo_upload = gr.Image(
|
75 |
+
label="Upload Actor Photo (for UI personalization)",
|
76 |
+
type="pil", # Pillow image object
|
77 |
+
sources=["upload"],
|
78 |
+
interactive=True
|
79 |
+
)
|
80 |
+
gr.Markdown("*(Note: Photo customization is for UI personalization. The LLM itself currently processes text only.)*")
|
81 |
+
|
82 |
+
with gr.Column():
|
83 |
+
query_input = gr.Textbox(
|
84 |
+
label="Ask for Acting Advice",
|
85 |
+
placeholder="e.g., How should Anna deliver her line 'I can't believe this...' to convey despair?",
|
86 |
+
lines=3
|
87 |
+
)
|
88 |
+
submit_btn = gr.Button("Get Advice")
|
89 |
+
output_text = gr.Textbox(label="LLM Advice", lines=7)
|
90 |
+
|
91 |
+
submit_btn.click(
|
92 |
+
fn=get_actor_advice,
|
93 |
+
inputs=[query_input, script_input, character_name_input],
|
94 |
+
outputs=output_text
|
95 |
+
)
|
96 |
+
|
97 |
+
gr.Markdown("---")
|
98 |
+
gr.Markdown("Powered by DeepSeek LLMs, Hugging Face, and Cloudflare.")
|
99 |
+
|
100 |
+
demo.launch(share=True)
|