import os import gradio as gr from huggingface_hub import InferenceClient api_key = os.getenv("HF_API_KEY") client = InferenceClient(api_key=api_key) # Create shared state for the textbox values class State: def __init__(self): self.context = "This Adrega component is designed to answer questions provided by API" self.question = "What does this component do?" state = State() '''def analyze(project_data, question): try: prompt = f"Analyze this project: {project_data}\n\nQuestion: {question}" messages = [ {"role": "system", "content": f"Context: {project_data}"}, {"role": "user", "content": question} ] response = client.chat.completions.create( model="Qwen/Qwen2.5-72B-Instruct", messages=messages, max_tokens=1000, stream=True ) answer = "" for chunk in response: answer += chunk['choices'][0]['delta']['content'] yield answer except Exception as e: print(f"Error details: {str(e)}") yield f"Error occurred: {str(e)}" ''' # Function to update textbox values def update_values(context, question): state.context = context state.question = question return state.context with gr.Blocks() as iface: # Create the components with the state values project_data = gr.Textbox(label="Project Data", lines=2, value=lambda: state.context) question = gr.Textbox(label="Question", lines=1, value=lambda: state.question) output = gr.Textbox(label="Output") # Create analyze button #analyze_btn = gr.Button("Analyze") # Connect the analyze function #analyze_btn.click( # fn=analyze, # inputs=[project_data, question], # outputs=output #) update_btn = gr.Button("Update") update_btn.click( fn=update_values, inputs=['test1', 'test2'], outputs=output ) # Add API endpoint for updating values iface.add_api_route("/update_values", update_values, methods=["POST"]) # Configure for external access iface.launch( server_name="0.0.0.0", # Allow external connections share=True, # Create public URL allowed_paths=["*"], # Allow CORS )