Update app.py
Browse files6 unmodified lines789101112132 unmodified lines16171819202122232425262728293031323334353637383918 unmodified lines5859606162636465666768696 unmodified lines# REPLACE "your_actual_token_here" with your real HuggingFace tokenos.environ["HUGGINGFACE_TOKEN"] = "your_actual_token_here"HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"HF_API_TOKEN = os.getenv("token2")headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}2 unmodified linesdata = {"inputs": prompt,"parameters": {"max_new_tokens": 512,"temperature": 0.1,"top_p": 0.95,"return_full_text": False}}response = requests.post(HF_API_URL, headers=headers, json=data)if response.status_code == 200:result = response.json()if isinstance(result, list) and len(result) > 0:if "generated_text" in result[0]:return result[0]["generated_text"]else:return str(result[0])else:return str(result)else:return f"HF API Error: {response.status_code} - {response.text}"# ---------------- PDF Processing ----------------status_message = "Upload a PDF to initialize QA system."18 unmodified lines# Try local QA first (using your Mistral model)local_answer = query_pdf(question)# Also try HF API as backuptry:hf_prompt = f"[INST] {question} [/INST]"hf_answer = query_hf_model(hf_prompt)return f"**Local Mistral Answer:** {local_answer}\n\n**HF API Answer:** {hf_answer}"except:# If HF API fails, just return local answerreturn f"**Answer:** {local_answer}"6 unmodified lines78910111213142 unmodified lines17181920212223242526272829303132333435363738394041424318 unmodified lines62636465666768697071726 unmodified lines# REPLACE "your_actual_token_here" with your real HuggingFace tokenos.environ["HUGGINGFACE_TOKEN"] = "your_actual_token_here"# Use a working model URL for HF Inference APIHF_API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"HF_API_TOKEN = os.getenv("token2")headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}2 unmodified linesdata = {"inputs": prompt,"parameters": {"max_length": 100,"temperature": 0.7,"return_full_text": False}}try:response = requests.post(HF_API_URL, headers=headers, json=data)if response.status_code == 200:result = response.json()if isinstance(result, list) and len(result) > 0:if "generated_text" in result[0]:return result[0]["generated_text"]else:return str(result[0])else:return str(result)else:return f"HF API Error: {response.status_code} - {response.text}"except Exception as e:return f"API request failed: {str(e)}"# ---------------- PDF Processing ----------------status_message = "Upload a PDF to initialize QA system."18 unmodified lines# Try local QA first (using your Mistral model)local_answer = query_pdf(question)# Also try HF API as backup (simpler prompt for DialoGPT)try:hf_answer = query_hf_model(question)return f"**Local Answer:** {local_answer}\n\n**HF API Answer:** {hf_answer}"except:# If HF API fails, just return local answerreturn f"**Answer:** {local_answer}"
app.py+21-18