Spaces:
Runtime error
Runtime error
Make it possible to update values via API
Browse files
app.py
CHANGED
|
@@ -5,6 +5,14 @@ from huggingface_hub import InferenceClient
|
|
| 5 |
api_key = os.getenv("HF_API_KEY")
|
| 6 |
client = InferenceClient(api_key=api_key)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def analyze(project_data, question):
|
| 9 |
try:
|
| 10 |
prompt = f"Analyze this project: {project_data}\n\nQuestion: {question}"
|
|
@@ -30,16 +38,33 @@ def analyze(project_data, question):
|
|
| 30 |
except Exception as e:
|
| 31 |
print(f"Error details: {str(e)}")
|
| 32 |
yield f"Error occurred: {str(e)}"
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Configure for external access
|
| 45 |
iface.launch(
|
|
|
|
| 5 |
api_key = os.getenv("HF_API_KEY")
|
| 6 |
client = InferenceClient(api_key=api_key)
|
| 7 |
|
| 8 |
+
# Create shared state for the textbox values
|
| 9 |
+
class State:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self.context = ""
|
| 12 |
+
self.question = ""
|
| 13 |
+
|
| 14 |
+
state = State()
|
| 15 |
+
|
| 16 |
def analyze(project_data, question):
|
| 17 |
try:
|
| 18 |
prompt = f"Analyze this project: {project_data}\n\nQuestion: {question}"
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
print(f"Error details: {str(e)}")
|
| 40 |
yield f"Error occurred: {str(e)}"
|
| 41 |
+
|
| 42 |
+
# Function to update textbox values
|
| 43 |
+
def update_values(context, question):
|
| 44 |
+
state.context = context
|
| 45 |
+
state.question = question
|
| 46 |
+
return state.context, state.question
|
| 47 |
+
|
| 48 |
+
with gr.Blocks() as iface:
|
| 49 |
+
# Create the components with the state values
|
| 50 |
+
project_data = gr.Textbox(label="Project Data", lines=2, value=lambda: state.context)
|
| 51 |
+
question = gr.Textbox(label="Question", lines=1, value=lambda: state.question)
|
| 52 |
+
output = gr.Textbox(label="Output")
|
| 53 |
+
|
| 54 |
+
# Create analyze button
|
| 55 |
+
analyze_btn = gr.Button("Analyze")
|
| 56 |
+
|
| 57 |
+
# Connect the analyze function
|
| 58 |
+
analyze_btn.click(
|
| 59 |
+
fn=analyze,
|
| 60 |
+
inputs=[project_data, question],
|
| 61 |
+
outputs=output
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Create an API endpoint for updating values
|
| 65 |
+
iface.load(fn=lambda: (state.context, state.question), outputs=[project_data, question])
|
| 66 |
+
gr.on(triggers=["update_values"], fn=update_values)
|
| 67 |
+
|
| 68 |
|
| 69 |
# Configure for external access
|
| 70 |
iface.launch(
|